GEt USer activity from AD

I have a CSV file with samaccountnames  - I want to run it thru Powershell to check to see when the users account was created , the last logon time stamp and days since it last log on
then have it exported to a csv file
gd6627Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FOXActive Directory/Exchange EngineerCommented:
Name the top of the column that has the samaccountnames-                      Samaccountname

Import-Csv 'filelocation\nameofcsv.csv | %{Get-ADUser $_.Samaccountname -properties * |  Select DisplayName,SamAccountname,whencreated,LastLogonDate,@{N='
LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}}, @{N='Days Since Last Logon'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}
ITguy565Commented:
$content = get-content c:\temp\sam.csv

$content | get-aduser -Properties * |select Name, createTimeStamp, 
    @{N = 'LastLogon'; E = {[DateTime]::FromFileTime($_.LastLogon)}},
    @{N = 'LastLogon in Days'; E = {[DateTime]::FromFileTime($_.LastLogn)}}, 
    @{N = 'Last Logon Days'; E = {$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}| export-csv -path c:\temp\test.csv

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITguy565Commented:
Since I was already writing it I decided to post it, but @fox Should get the points. Should be noted however that my script completes the export-csv as well..
Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

Sara TeasdaleCommented:
Here's the PowerShell command:

Get-ADUser <UserName> -Properties whenCreated | Format-List Name,whenCreated

Checkout quick reference guide we created about AD auditing - it includes instructions and event IDs:

http://www.netwrix.com/download/documents/Native_AD_Auditing_Cheat_Sheet.pdf

You can simply use dsquery * command: http://technet.microsoft.com/en-us/library/cc754232(v=ws.10).aspx

For formatting to readable format, use w32tm /ntte to format the output of your dsquery command.

For more info here: http://social.technet.microsoft.com/Forums/en/ITCG/thread/19d35656-2be4-4665-822e-34c111e07da2

If you want to get the exact date for the last logon, you can query lastlogon attribute (which is not replicated) on each DC and keep only the highest value of the queries as it represents the last logon.

For Powershell scripting, this should help: http://blog.mattvogt.net/powershell-last-logon-timestamp-for-single-ho

And if you want to dig further and understand who created it (and possibly why), NetWrix AD Change Reporter ( https://www.netwrix.com/active_directory_auditing.html ) can be of great value for you (the tool audits who changed, created or deleted what AD object, when and where)
gd6627Author Commented:
@Fox Am getting the following error:

ERROR: Import-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "LastLogon;
ERROR: E={[DateTime]::FromFileTime($_.LastLogon)}}, @{N=Days" to type "System.Char". Error: "String must be exactly one character long."
GEtUSerStatus.ps1 (16, 70): ERROR: At Line: 16 char: 70
ERROR: + ... DisplayName,SamAccountname,whencreated,LastLogonDate,@{N='LastLogon';
ERROR: +                                                               ~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidArgument: (:) [Import-Csv], ParameterBindingException
ERROR:     + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportCsvCommand
ERROR:
ITguy565Commented:
gd6627,

Please try mine and let me know the output or errors associated with it.
gd6627Author Commented:
Ok @ItGuy565 trying now , Thanks
FOXActive Directory/Exchange EngineerCommented:
I see I didn't close my quote after the .csv file name and I needed another closing bracket at the end.  Try the below with your filename.

Import-Csv 'filelocation\nameofcsv.csv' | %{Get-ADUser $_.Samaccountname -properties * |  Select DisplayName,SamAccountname,whencreated,LastLogonDate,@{N='
LastLogon'; E={[DateTime]::FromFileTime($_.LastLogon)}}, @{N='Days Since Last Logon'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}} | ft | Out-file 'c:\temp\results.txt'
gd6627Author Commented:
since you are using a $content  do I need a header in the csv file for samaccount?
gd6627Author Commented:
Fox is there a way that the output can be ina CSV file format ?
gd6627Author Commented:
Can you explain what the Letter E= and N= stand for
gd6627Author Commented:
@ITGuy565  am getting the following as a result heres a sample the "last logon in days" columm is blank all the way down and the "last logon day has some big numbers  please see attached sample
testoutput.xlsx
gd6627Author Commented:
I did receive to good solutions but I had questions that were never followed up on
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.