More power PowerShell: Working with the Out-String cmdlet

Recently, I have spent quite a bit of time working with PowerShell’s Out-String cmdlet. The basic idea behind using this cmdlet is that sometimes text output within PowerShell can become a bit unwieldy. The Out-String cmdlet gives you an easy way of taking text and assembling it into a string that you can then display or manipulate on an as needed basis. Let me show you a few of the things that you can do with this powerful cmdlet.

Working with text files

One of the ways that I like to use the Out-String cmdlet is to map the contents of a text file to a variable. As you may already know, PowerShell makes it relatively easy to display the contents of a text file on screen. You can use the Get-Content cmdlet along with the text file’s path and file name. If for example, I wanted to display a text file named C:\Data\License.txt, I would use this command:

Get-Content -Path C:\Data\License.txt

The thing about this command is that it only displays the contents of a text file. It isn’t well suited to allowing you to manipulate the text file’s contents (although you can do it in a roundabout way). As an alternative, you can use the Out-String cmdlet to not only display the contents of a text file but also to format the output to suit your needs. So let’s take a look at a couple of examples.

If you look at the screenshot below, you can see that I have used the Get-Content cmdlet to display the contents of a text file named License.txt. This file is simply the Python license file, but I shortened it for the sake of getting the file’s entire contents to fit on the screen.

out-string

Now, if you look at the next screen capture, you can see that I have appended the Out-String cmdlet to the command that I already used. At first, it would seem that I’m not doing anything different because the output is exactly the same. The important thing, however, is that the text is now being treated as a string, and strings can be manipulated.

out-string

Now let’s suppose that for whatever reason, I only want to display the first paragraph of the text file. The first step in doing so is to map the contents of the text file to a variable. I will call this variable $A. Here is the command:

$A=Get-Content -Path C:\Data\License.txt

Next, I pipe the $A variable into the Out-String command. I am also going to append the -Stream command. This causes each block of text (separated by a line break) to be treated as a separate stream. Now, I simply have to tell PowerShell which paragraph I want to display. Since the first word of the target paragraph is Python, I will add Select-String “Python” to the end of the command. Here is what the command looks like:

$A | Out-String -Stream | Select-String Python

You can see these commands in action in the screenshot below. I have also displayed the full contents of the $A variable so that you can see that I am indeed displaying a subsection of the text file.

out-string
One thing to keep in mind is that because I am working with string output, I can manipulate that output in a variety of ways. For instance, I might replace some characters with alternate text, or I might truncate part of the output. PowerShell provides a number of options for string manipulation.

Converting command output to a string

Another thing that you can do with the Out-String cmdlet is to combine the contents of multiple variables into a single string. While writing an unrelated article, I created two variables named $File1 and $File2. These two variables contain the MD5 hash for two different files. If I wanted to, I could combine the contents of these variables into a single string. Here is how it is done:

$A=($File1,$File2 | Out-String)

You can see the output in the screenshot below. As you look at the output, you will notice that even though the $A variable contains both hashes, they are listed on separate lines. The documentation for the Out-String cmdlet indicates that you can remove the line break by appending the NoNewLine parameter. In practice, however, this parameter is not supported (at least not in the Windows 10 version of PowerShell that I am using).

out-string

Selecting an item from a list

There is one more trick that I want to show you. If you work with PowerShell, you are probably familiar with commands like Get-Process, Get-Service, or Get-VM that display long lists of items in their output. You can use the Out-String cmdlet in conjunction with such commands. In doing so, you can write all or part of the output to a string variable. The advantage to doing this is that you can filter the output in pretty much any way that you want.

If you look at the screenshot below, you can see that I have created a variable named $A and mapped it to the Get-VM cmdlet. When I output the variable’s contents, I am presented with a list of the VMs on my computer.

out-string
My $A variable contains a list of my VMs.

Now, let’s output this to a string. Just to make things interesting though, let’s concatenate the output by displaying only the first 50 characters. The command for doing so is:

Out-String -InputObject $A -Width 50

You can see the output in the screenshot below.

out-string
One more thing that I can do is to filter the output to show only one of the VMs on the list. Here is the command that I used:

Out-String -InputObject $A -Width 50 -Stream | Select-String “VM1”

As you can see in the screenshot below, the output is not only concatenated, it has also been filtered to show only VM1.
out-string

Get creative with Out-String

As you can see, the Out-String command allows you to do a variety of creative things within your PowerShell scripts. In fact, this article really only just begins to scratch the surface of what you can do with strings in PowerShell.

Featured image: Shutterstock

About The Author

3 thoughts on “More power PowerShell: Working with the Out-String cmdlet”

  1. Excuse me, but that is now how you PowerShell.

    The entire benefit of PowerShell over older shells is that you are working with objects as opposed to text, so commands like:

    Out-String -InputObject $A -Width 50

    really hurt. The correct way to do that would be:

    $a | Format-Table Name, State, CPUUsage

    You don’t need to delimit PowerShell output by tabs, char count, commas or anything. $a contains an array of objects, each having a Name and State property among others. Format-Table can read objects and it can selectively display some of their properties in a neatly formatted table for the human to look at.

    Similarly, as opposed to:

    Out-String -InputObject $A -Width 50 -Stream | Select-String “VM1”

    you would do:

    $a | Where-Object { $_.Name -eq ‘VM1’ }

    to filter out the object whose “Name”-property is “VM1”. Although really, you’d want to just do:

    Get-VM -Name “VM1”

    to filter out the one you want right at the source. This is faster.

    PowerShell does not work like an old UNIX shell. It is a couple decades newer.

    1. What if you don’t know the name of property in an object? What if there could be multiple properties? I am searching an AD user object or I am searching an Exchange mailbox. Suzy Smith got married and I need to change all properties that include ‘Smith’ to ‘Jones’. I don’t know all the properties that might include that old last name. Sometimes PowerShell’s objects complicate instead of simplify the result.

      Thanks, Brien Posey. Your thoughts led me to
      Get-RemoteMailbox ‘ssmith’ | Select * | Out-String -Stream | Select-String -Pattern ‘smith’
      I now have an idea of the dozen properties that need to be updated. The solution has issues, but I am much closer to resolving instead of fighting with properties inside objects. Sometimes an “old UNIX shell” approach gets the job done faster…

      1. and for all the PowerShell die-hards, I figured out this way to accomplish similar results.

        $strProperties = ”
        $objUser = Get-RemoteMailbox ssmith
        ForEach ($objProperty in $objUser.PSObject.Properties) {
        If ($objProperty.Value -like “*smith*”) {
        If ($strProperties -eq ”) {
        $strProperties = $objProperty.Name
        } Else {
        $strProperties += “, $($objProperty.Name)”
        }
        }
        }
        Get-RemoteMailbox ssmith| Select-Object -Property ($strProperties -Split ‘, ‘)

        Is there an easier way?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top