locked
How to swap values TRUE and FALSE in an exported CSV file RRS feed

  • Question

  • I've got a script that exports all AD users.  It's only two columns in a csv file.  The first is username and the second is Enabled.

    Get-ADUser -Filter * -Properties * | Select @{Name="User ID";Expression={$_.SamAccountName}},@{Name="Disabled";Expression={$_.Enabled}} |export-csv -Path "C:\Path\Output.csv"

    Get-ADUser returns a true or false value for the property "Enabled".  However, the person who uses the csv needs the column header to say Disabled and then return true or false.  I can change the column header no problem (shown above), but now i need some way to swap 'true' and 'false' in the csv.  Make sense?

    I tried adding a second line..

    get-content "C:\Path\Output.csv" | ForEach-Object -Process {$_ -replace 'True','False'} |set-content "C:\Path\UpdatedOutput.csv"

    ..But all that did was create a new csv file with all the values in the Disabled column set to False, not swap them around like i need it to do.

    Any help is greatly appreciated.

    Friday, March 6, 2020 4:57 PM

All replies

  • Please use the code posting tool to post code!!

    $properties = @(
        @{n = 'User ID'; e = {$_.SamAccountName}},
        @{n = 'Disabled';e = {if($_.Enabled){$false}else{$true}}}
    )
    Get-ADUser -Filter * -Properties * | 
        Select $properties |
        export-csv C:\Path\Output.csv


    \_(ツ)_/

    Friday, March 6, 2020 5:23 PM
  • Perfect!  Thanks much! :)
    Friday, March 6, 2020 8:03 PM
  • Or this:

    $properties = @(
        @{n = 'User ID'; e = {$_.SamAccountName}},
        @{n = 'Disabled';e = {-not $_.Enabled)
    )
    Get-ADUser -Filter * -Properties * | 
        Select $properties |
            export-csv C:\Path\Output.csv


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, March 6, 2020 8:32 PM