Powershell

How to Use Out-String (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

PowerShell is a Windows utility that is used by administrators to perform tasks that need elevated permission from the administrators. The “Out-String” cmdlet in PowerShell converts input objects into output strings. It is one of the “Microsoft.PowerShell.Utility” modules and formats the output in the PowerShell console. When it operates on an array, it converts it into a string.

In this post, we will illustrate the “Out-String” cmdlet in PowerShell.

How to Use Out-String (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

To use the “Out-String” cmdlet in PowerShell, first, write the cmdlet or input that needs to be converted into a string. Then, place the pipeline with the “Out-String” cmdlet to perform the conversion. Let’s uncover details about the stated cmdlet with the aid of examples.

Example 1: Use the “Out-String” Cmdlet to Output it as a String
First, place the desired cmdlet whose output needs to be converted into a string. Then, pipe it to the “Out-String” cmdlet:

Get-Service | Out-String

Example 2: Use the “Out-String” Cmdlet to Work with Objects
Execute this command to get an alias-related command in PowerShell:

Get-Alias | Out-String -Stream | Select-String -Pattern "cls"

Example 3: Use the “Out-String” Cmdlet to Save the Output to a File
To save the content of the particular cmdlet into a file, execute the provided cmdlet:

Get-Service | Out-String | Set-Content -Path "C:\Docs\New_1.txt"

Now, run the provided command to verify whether the content was stored in a file or not:

Get-Content -Path "C:\Docs\New_1.txt"

That’s it! We have provided information about the “Out-String” cmdlet in PowerShell.

Conclusion

PowerShell’s “Out-String” cmdlet converts the input text, objects, or command into a string. Moreover, it can also format the output in the console. This post has elaborated on the “Out-String” cmdlet with the help of various examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.