Ivan Konopelko
Ivan Konopelko

Reputation: 1

Get-ADComputer -Filter

I have a script that uploads data about a PC with the last activity more than 100 days, I can’t upload it as a normal table and I don’t understand how to add 2 distributionname + description fields

Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate |      Where { $_.LastLogonDate -LT (Get-Date).AddDays(-100) } |     Select-Object Name, OperatingSystem, LastLogonDate  | Out-File "\\Client\C$\Users\computer_ad.csv"   -encoding Unicode -Delimiter ";"

Upvotes: 0

Views: 1307

Answers (1)

Theo
Theo

Reputation: 61048

You should not use Out-File to save as CSV. There is a special cmdlet for that called Export-Csv

Also, it is better to set the reference date to midnight using .Date when comparing to the LastLogonDate.

By default, Get-ADComputer returns these properties: DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName and for anything on top of that you need to specify it in the -Properties parameter.

As for attribute Description, that's easy enough, but what do you mean by distributionname ?? I'm guessing you want the DistinguishedName name there:

$refDate = (Get-Date).AddDays(-100).Date  # set this to midnight
Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate, Description | 
Where-Object { $_.LastLogonDate -lt $refDate } | 
Select-Object Name, OperatingSystem, LastLogonDate, Description, DistinguishedName | 
Export-Csv -Path "\\Client\C$\Users\computer_ad.csv" -Delimiter ';' -NoTypeInformation

If you really want the file to be encoded in UTF16-LE (Unicode), you can add that to the Export-Csv line: -Encoding Unicode, although UTF8 is more commonly used.

Upvotes: 1

Related Questions