Newbee Help with Strings Please

Good Afternoon All,

I’m a complete newbee to powershell trying to get information into a string variable.

The String looks like this.

$LD = Get-ADUser -Filter {name -eq $SelectUser}|Select-Object -Property DistinguishedName|ft -AutoSize

What I’m trying to do is to store the output (currently being written to the screen) the the variable $LD

I know that the command;

Get-ADUser -Filter {name -eq $SelectUser}|Select-Object -Property DistinguishedName|ft -AutoSize

returns what I want, but cant seem to get the results into a string/

But all I get is Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData"

What do I need to do here please?

I’m a little lost!

With thanks

Format-Table does not produce strings, it produces formatting directives, so that’s what you’ve got in $LD. You’re thinking Format-Table produces strings because you see text on the screen, but there’s stuff going on under the hood that’s messing you up :).

Try piping it to Out-String instead of Format-Table.


$LD = Get-ADUser -Filter {name -eq $SelectUser} | Select-Object -ExpandProperty DistinguishedName

Would probably be what you want, without the table header Format-Table produces.

See also And, if you’d like to just not ever get plagued by a “gotcha” again, check out “Learn Windows PowerShell in a Month of Lunches, 3rd Edition.”