Solved

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

Posted on 2011-09-02
11
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

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 70

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 500 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

734 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