Solved

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

Posted on 2011-09-02
11
524 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
 
LVL 68

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article will help you understand what HashTables are and how to use them in PowerShell.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now