Join Powershell result

Hi,

Simple question, but cant seem to figure it working with the -join function. Im looking to join results from a powershell command using a single command (not outputting to a variable and then calling both variables), separated by a hyphen with no extra spaces. For example,

Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod | ft -HideTableHeaders

Should Return:
C:-FullyDecrypted-None

 

 

Hi Joe,

 

It looks like you are treating the output of the command as a single string and then trying to manipulate a strign into the format you need.

Most PowerShell commands output data as an object that may or may not have named properties.

Lets take a look at the output of the command (sans the Format-Table)

Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod

MountPoint   VolumeStatus EncryptionMethod
----------   ------------ ----------------
C:         FullyDecrypted             None

So we get an object with the named properties

MountPoint VolumeStatus and EncryptionMethod

to get the output into the format you would like we just make a new string by calling these named properties from the output of the command i saved as the

$OutputObject Variable

$OutputObject = Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod

$OutputString = $OutputObject.MountPoint + '-' + $OutputObject.VolumeStatus + '-' + $OutputObject.EncryptionMethod

C:-FullyDecrypted-None

 

I hope that answers your question (and that the code formatting is acceptible haha)

Ah, makes perfect sense. What if if i use “Out-String” - something like the command below. Would that still be considered an object?

(Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod | ft -HideTableHeaders | Out-String) -join ‘’

Another approach using string format:

$results = Get-BitLockerVolume | Select MountPoint,VolumeStatus,EncryptionMethod

'{0}-{1}-{2}' -f $results.MountPoint, $results.VolumeStatus, $results.EncryptionMethod

Format-Table (ft) is almost always going to break things when exporting because you are changing it from an object to a a formatted string. In short, Format-Table breaks the pipeline.

You are taking stored in a table (PSObject) and trying to join the items into a string. The individual elements need to referenced in some manner. You would do the same thing in Excel for CONCAT referencing cells to join into a string.

[quote quote=133533]Ah, makes perfect sense. What if if i use “Out-String” – something like the command below. Would that still be considered an object?

(Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod | ft -HideTableHeaders | Out-String) -join "

[/quote]

Yes it would because everything is an object in powershell, and a String is simply a type of object.

While piping the commands to a Format-Table (ft) and converting the result to a string works for you now, you cant be sure it will always give you the output you want. Rob is absolutley correct about Format-Table, the only time i ever use it is when im trying to make sure an array contains the data i want.

Keeping the result as an object and then buiding a string around referencing that object is a lot easier to troubleshoot and gives you a lot of flexibility if you want to do something more that just output that data into a text file.

Just out of curiosity is this part of a larger script?

Or are you just building out a text report of machines in your environment?

 

It’s almost like you’re making a csv with ‘-’ as the delimiter. But it adds doublequotes.

Get-BitLockerVolume | select MountPoint,VolumeStatus,EncryptionMethod | convertto-csv -delimiter '-'

Thank you all for the response and info. Really helpful!