Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I write output  fields to a file with Powershell?

Posted on 2011-09-02
11
Medium Priority
?
538 Views
Last Modified: 2012-05-12
There are many things I love about Powershell, but it seems overly perplexing to write to a file? I tried " around the fields, but then my variables all change.  Please advise.  When I write-host -- It works great to the screen.
Thanks
C Emmons
Import-Module ActiveDirectory
$logfile="./useraudit.csv"
$u = (Get-ADUser emmonsc | Get-ADObject -Properties givenname,sn,canonicalname,description,mail,createtimestamp,lastLogon,title,department,employeeid)
$time = $user.LastLogon
   
 
$dt = [DateTime]::FromFileTime($time)
 
foreach ($user in $u) {write-output ($user.sn,$user.givenname,$user.title,$user.department,$user.canonicalname,$user.description,$user.mail,$user.createtimestamp,$dt,$user.employeeid) |out-file -filepath $logfile -encoding ascii -append}

Open in new window

0
Comment
Question by:apsutechteam
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 4

Expert Comment

by:LHFoods
ID: 36476391
Am I correct in assuming that you wish to view a list of users based on last logon?  If so there is a nice little program called OldCmp that works beautifully for this.  You can find it here:
http://www.joeware.net/freetools/

Good Luck
0
 
LVL 3

Expert Comment

by:LindyS
ID: 36477017
You need Quest Active Roles Management Shell for this command, but it may get what you need.

Get-QADUser emmonsc | Format-Table -autosize name,sn,canonicalname,description,mail,createtimestamp,lastLogon,title,department,employeeid | Out-File UserAudit.txt -width 375 -append
0
 
LVL 49

Expert Comment

by:Akhater
ID: 36478860
all you need to do is use the select-object and pipe it with the export-csv

so, in your case, it would be something like

$u | select-object givenname,title | export-csv c:\file.csv
Import-Module ActiveDirectory
$logfile="./useraudit.csv"
$u = (Get-ADUser emmonsc | Get-ADObject -Properties givenname,sn,canonicalname,description,mail,createtimestamp,lastLogon,title,department,employeeid)
$time = $user.LastLogon
   
 
$dt = [DateTime]::FromFileTime($time)
 
$u | select-object sn,givenname,title,department,canonicalname,description,mail,createtimestamp,$dt,employeeid) | export-csv $logfile

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
LVL 71

Expert Comment

by:Qlemo
ID: 36480014
The closing parens in the last line needs to be removed. select-object, as shown, is the way to go. You might want to change CSV parameters, like omitting writing type info (which is only useful for PS itself), a header, using a different delimiter aso.
0
 

Author Comment

by:apsutechteam
ID: 36489937
First, let me apologize - the reference to $user. should be $u. .  Also, my end goal is to get-aduser *.  I do have only one user in this code -- but that was just for testing.  Therefore, I run into the problem of how to convert the lastlogon info when exporting with export-csv.  Can this be used line by line?
0
 
LVL 49

Expert Comment

by:Akhater
ID: 36491444
sorry I didn't understand your last post !

did you try my code?
0
 

Author Comment

by:apsutechteam
ID: 36491521
Yes, but I don't know how to convert the lastlogon to a readable format with the export-csv. When you dump  a large group of records with export-csv - how do you do conversions?  I learning Powershell, and usually loop through and write out line by line.  Can that be done with export-csv or is there a better way?
Thanks
0
 
LVL 49

Expert Comment

by:Akhater
ID: 36491604
sorry maybe it is me being a bit thick!

you mean my code worked, you just need to change the lastlogon to something more readabel ?
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 36491981
Akhater,

Your post is based on output of a single user. apsutechteam is just asking how to format a muliple-object output with select-object / export-csv. That is, bringing the [DateTime]::FromFileTime into the select-object.
0
 
LVL 49

Expert Comment

by:Akhater
ID: 36492278
lol that's totally silly from me :D sorry and thanks Qlemo

apsutechteam  please find below
0
 
LVL 49

Accepted Solution

by:
Akhater earned 2000 total points
ID: 36492285
lol that's totally silly from me :D sorry and thanks Qlemo

apsutechteam  please find below

Import-Module ActiveDirectory
$logfile="./useraudit.csv"

$users = get-ADuser -filter * | Get-ADObject -Properties givenname,sn,canonicalname,description,mail,createtimestamp,lastLogon,title,department,employeeid

$users | select-object sn,givenname,title,department,canonicalname,description,mail,createtimestamp,@{expression={([DateTime]::FromFileTime($_.LastLogon)).toString()};label="LastLogon"},employeeid  | export-csv $logfile

Open in new window

0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Loops Section Overview
Screencast - Getting to Know the Pipeline

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question