Newbee Help with Strings Please

Welcome Forums General PowerShell Q&A Newbee Help with Strings Please

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
1 year, 11 months ago.

  • Author
    Posts
  • #61164

    Participant
    Points: 0
    Rank: Member

    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

  • #61165

    Keymaster
    Points: 1,673
    Helping HandTeam Member
    Rank: Community Hero

    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.

    Alternately,

    $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 https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/format-right.html. 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."

The topic ‘Newbee Help with Strings Please’ is closed to new replies.