How to use two echo commands to print data on one line using PowerShell

前端 未结 3 1682
南方客
南方客 2021-01-23 18:18

Below is the code I\'m using to find users created date and last logon date which is then written to a .csv file.

$users=Get-ADuser -SearchBase \"OU=testou1,dc=U         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 19:07

    I would create an an array of objects with name and value properties. That will export nicely to CSV.

    Bear in mind I'm writing this on a phone, I'll check it in my ISE later, but I've amended your code below:

    $users=Get-ADuser -SearchBase "OU=testou1,dc=US,dc=ITOPS,dc=COM" -Filter * -properties samaccountname,lastlogondate,Created    
    
    $array = @() #declare the array outside the loop
    
    $forloop = foreach($user in $users) {    
    $arrayrow = New-Object System.Object
         $arrayrow | Add-Member -type NoteProperty -name User -value $user.samaccountname    
          $arrayrow | Add-Member -type NoteProperty -name Created -value  $user.created    
        $arrayrow | Add-Member -type NoteProperty -name LastLogonDate -value $user.lastlogondate    
      $array += $arrayrow
        }    
    $array | export-csv  -notype c:\bin\exporting.csv 
    

提交回复
热议问题