Asked by:
Powershell export CSV with specific attributes

Question
-
Hi,
Im trying to export CSV file from AD on a specific OU with the following information
Full Name
Description
Office
Also is there a way to specify users that has not been logged in 90 Days with the information?
Full Name
Description
OfficeWednesday, November 15, 2017 12:01 PM
All replies
-
What have you tried so far? what errors or issues are you getting with your script?Wednesday, November 15, 2017 12:17 PM
-
Hi,
You should approach this forum when you are facing an error in the script that you have written to accomplish some task.
Best place to search for readily available scripts is technet library.
check this link lastlogon
Upvote if it helps you in attending your issue
(66,65,83,65,84,73|%{[char]$_})-join''
Wednesday, November 15, 2017 12:43 PM -
Hi,
IM using
# Gets time stamps for all User in the domain that have NOT logged in since after specified date # Mod by Tilo 2013-08-27 import-module activedirectory $domain = "domain" $DaysInactive = 90 $time = (Get-Date).Adddays(-($DaysInactive)) # Get all AD User with lastLogonTimestamp less than our time and set to enable Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp | # Output Name and lastLogonTimestamp into CSV select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv OLD_User.csv -notypeinformation
Its working good, but it does not include Office, and Department.
Also, Im looking for a script thats takes out Full name, office and department- Edited by Happywilli Wednesday, November 15, 2017 1:02 PM
Wednesday, November 15, 2017 1:02 PM -
You need to add the office and department attributes to the filter.
Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp,office,department|
And to the output.
select-object Name,office,department,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv OLD_User.csv -notypeinformation
Wednesday, November 15, 2017 1:26 PM -
# Gets time stamps for all User in the domain that have NOT logged in since after specified date # Mod by Tilo 2013-08-27 import-module activedirectory $domain = "domain" $DaysInactive = 90 $time = (Get-Date).Adddays(-($DaysInactive)) # Get all AD User with lastLogonTimestamp less than our time and set to enable Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp, Department, Office | # Output Name and lastLogonTimestamp into CSV select LastLogonTimeStamp, Department, Office | export-csv OLD_User.csv -notypeinformation
(66,65,83,65,84,73|%{[char]$_})-join''
- Edited by BASATI Thursday, November 16, 2017 6:45 AM
Wednesday, November 15, 2017 2:18 PM -
Hi,
>> Im trying to export CSV file from AD on a specific OU with the following information
Full Name
Description
Office
Based on my research, I recommend use -SearchBase parameter to filter a specific OU. The following demo command for your reference, hope it is helpful to you:
Get-ADUser -Filter * -Properties Description,Office -SearchBase 'OU=Demo,DC=contoso,DC=com' | Select-Object Name,Description,Office | Export-Csv -Path 'D:\OLD_User.csv' -NoTypeInformation
>> Also is there a way to specify users that has not been logged in 90 Days with the information?
Full Name
Description
Office
In this case, I'd like to explain that LastLogonDate is a converted version of LastLogontimestamp, it may be easier to use. For your reference:
Get-ADUser -Filter * -Properties Description,Office,LastLogonDate -SearchBase 'OU=TEST,DC=test,DC=lab' | Where-Object {$_.LastLogonDate -lt (get-date).adddays(-90)} | Select-Object Name,Description,Office| Export-Csv -Path 'D:\OLD_User.csv' -NoTypeInformation
You may refer to the following links for details:
Understanding the AD Account attributes - LastLogon, LastLogonTimeStamp and LastLogonDate
https://social.technet.microsoft.com/wiki/contents/articles/22461.understanding-the-ad-account-attributes-lastlogon-lastlogontimestamp-and-lastlogondate.aspx
If you need further help, please feel free to let us know.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Albert LingMicrosoft contingent staff Wednesday, November 22, 2017 3:06 AM
Thursday, November 16, 2017 6:15 AM -
Hi,
Just checking in to see if the information provided was helpful. Does the script work?
Please let us know if you would like further assistance.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comMonday, November 20, 2017 7:52 AM -
Hi,
I am checking how the issue is going, if you still have any questions, please feel free to contact us.
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Appreciate for your feedback.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comWednesday, November 22, 2017 3:05 AM