Solved

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

Posted on 2011-09-02
11
530 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 69

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 69

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…

726 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