Removing White Space in Array output

Welcome Forums General PowerShell Q&A Removing White Space in Array output

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

3 years, 4 months ago.

  • Author
  • #33789

    Topics: 4
    Replies: 4
    Points: 0
    Rank: Member

    Hello Experts,

    I am having an issue exporting machine sids so I can properly identify that there are no duplicate sids within my environment and keep a archive of machines.

    There is a sample of the code that I am playing with.

    First, I enter in this cmdlet:

    Get-ADComputer -Filter "*" | where {$_.Name -match "client"} | select SID | ft -HideTableHeader >>results1.txt

    See results1.txt.

    As you can see from results1.txt, there are white spaces all over the output. So I then decided to use trim the data by creating a custom variable.

    $a = Get-ADComputer -Filter "*" | where {$_.Name -match "client"} | select SID | ft -HideTableHeaders | Out-String
    $a.Trim() >>results2.txt

    See results2.txt

    As you can see in results2.txt, I successfully trimed the last value however, the first value still has some white space within the output.

    I then proceed to run this output but now it merges all the data.

    $b = $a.Trim()
    $b -replace "\s",""

    If anyone has any suggestions on what I can do, it would be greatly appreciated.

    Thank you.

  • #33790

    Topics: 2
    Replies: 95
    Points: 537
    Rank: Major Contributor

    Here's the thing, the Format-* commands are best left for displaying textual information inside your console — on the screen. When we start putting data into files, we typically (practically always) forgo this set of cmdlets. Enter my example below into your console and see what you get. Replace COMPUTER with the name of a single machine, as it's best to test with one, before you return them all (*).

    (Get-ADComputer -Filter {Name -eq 'COMPUTER'}).SID.Value | Out-File -FilePath results.txt

    Edit: Another thing to consider, as I've done above, and now below, is to use the -Filter parameter of Get-ADComputer to do your filtering, instead of filtering with Where-Object. There are some really sound reasons as to why you should do it this way. While you can't use -match, you can use other comparison operators, such as -eq and -like. Take a look.

    (Get-ADComputer -Filter {Name -like '*client*'}).SID.Value | Out-File -FilePath results.txt

The topic ‘Removing White Space in Array output’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort