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
3
  • 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. :( Commented Apr 21, 2015 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. Commented Apr 21, 2015 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. Commented Apr 22, 2015 at 1:51

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

# 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 why not just have the one echo line then? echo "$($user.samaccountname) $($user.created) $($user.lastlogondate)"?
thanks its working fine but it is giving in same column. is the any possible way to print in new column of same line
1

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 

3 Comments

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?
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.
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.
1

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

3 Comments

while im executing the above script there is no end result.It is like improper running of script continuously without end
Sorry, I didn't copy the second brace in properly. I corrected it now.
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;??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.