1

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=US,dc=ITOPS,dc=COM" -Filter * -properties samaccountname,lastlogondate,Created    
$forloop = foreach($user in $users) {    
         echo $user.samaccountname    
          echo $user.created    
        echo $user.lastlogondate    
        }    
$forloop | Out-file c:\bin\exporting.csv -Append 

This code prints each item on a new line like this:

username  
created date  
last logon date  

But I want it all on the same line with different columns:

username createdDate lastLogonDate
Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
user2925298
  • 79
  • 1
  • 2
  • 9
  • 1
    Good morning! In order to receive help here you really need to post code that shows us what you have already tried. What you have posted here is really a request for someone else to write the script that you need. :( – David Hoelzer Apr 21 '15 at 12:44
  • @DavidHoelzer the code is in the question. `echo 'hai'; echo 'hello'` I think he has a clear issue. Perhaps it is a dup but all the components are there. – Matt Apr 21 '15 at 12:52
  • We really need to know what is provoking the question. Tell the problem you want to solve, now _how_ you think you need to solve it. – Bill_Stewart Apr 22 '15 at 01:51

3 Answers3

3

Just so you know echo in PowerShell is actually an alias for Write-Output.

PS Z:\> get-alias echo

CommandType     Name                                               ModuleName                                                                                
-----------     ----                                               ----------                                                                                
Alias           echo -> Write-Output   

If you look at the TechNet article it does not natively support any append type parameter or newline suppression. That does not mean you can't do it. Just not without any help.

echo/Write-Output would just be sending data to the output stream. Is there a reason you need your text to do that? Is there by chance a better example you can provide of what you are trying to accomplish? FYI there are other cmdlets at work behind the scenes that being used as well like Out-Default

Write-Host is really what you want. Yes I am aware you said you didn't want it but I wanted you to see for sure.

PS Z:\> Write-host "Hai" -NoNewline; Write-Host "Hello"
HaiHello
Matt
  • 45,022
  • 8
  • 78
  • 119
  • # to find users created date and last logon date and upload to .csv $users=Get-ADuser -SearchBase "OU=testou1,dc=US,dc=ITOPS,dc=COM" -Filter * -properties samaccountname,lastlogondate,Created $forloop = foreach($user in $users) { echo $user.samaccountname echo $user.created echo $user.lastlogondate } $forloop | Out-file c:\bin\exporting.csv -Append – user2925298 Apr 22 '15 at 04:02
  • @user2925298 why not just have the one echo line then? `echo "$($user.samaccountname) $($user.created) $($user.lastlogondate)"`? – Matt Apr 22 '15 at 04:07
  • thanks its working fine but it is giving in same column. is the any possible way to print in new column of same line – user2925298 Apr 22 '15 at 04:11
0

I took the liberty to use some PowerShell features to output the data I think you are actually looking for. I am putting the properties into a custom object and leave output for the end of the script. This makes code reuse easier in the future as you refactor the script. I also used the native CmdLet to export the CSV in the proper format. You don't actually need to use the $forloop variable, because the foreach would put all of the objects into the pipeline, but I left it in because it can make things more readable.

 $users=Get-ADuser -SearchBase "OU=testou1,dc=US,dc=ITOPS,dc=COM" -Filter * -properties samaccountname,lastlogondate,Created    
    $forloop = foreach($user in $users) {    
             [pscustomobject]@{
                samaccountname=$user.samaccountname;
                created=$user.created;
                lastlogondate=$user.lastlogondate    
            }
          }    
    $forloop | Export-Csv -Path c:\bin\exporting.csv
John Hubert
  • 2,204
  • 2
  • 17
  • 18
  • while im executing the above script there is no end result.It is like improper running of script continuously without end – user2925298 Apr 22 '15 at 05:32
  • Sorry, I didn't copy the second brace in properly. I corrected it now. – John Hubert Apr 22 '15 at 05:36
  • ya its working fine now. thanks a lot. I am new to powershell.. can you please explain what is "[pscustomobject]@" used for and why you have given samaccountname=$user.samaccountname;?? – user2925298 Apr 22 '15 at 08:29
0

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 
Graham Gold
  • 2,435
  • 2
  • 25
  • 34
  • 1
    its working perfectly fine and im getting output as required.. thanks a lot. As im very new to powershell,can you please explain function of the terms $array and $arraynow clearly so that i can reuse them. and in the above code is $forloop still required? – user2925298 Apr 22 '15 at 08:09
  • Glad that done the trick for you. First of all, you don't need to keep the `$forloop = ` but as you've seen it does no harm, the `foreach` code after it still runs fine. $array and $arrayrow are both just variables that could be named however you want, I just used names that remind you what they are for. – Graham Gold Apr 22 '15 at 18:09
  • To create an empty array just assign a variable the value `@()` - special syntax in powershell. To create an object you assign a variable the value `New-Object System.Object`. You add properties to the object by piping (`|`) the variable to the `Add-Member` cmdlet, specifying type `NoteProperty`, a name and then a value. Finally to add the object to the array using the `+=` operator. It adds what is on the right of it to whatever is on the left. – Graham Gold Apr 22 '15 at 18:13