Solved

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

Posted on 2011-09-02
11
536 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 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

635 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