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 ";"
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.
Related
I'm completely new to Powershell and trying to learn as I go. I have a requirement to find all AD users whose passwords have not been reset within the last 365 days, I also need to pull various other fields such as lastlogontimestamp, manager, cn, distinguishedname etc. I have tried the code below and it will show some results in the powershell window but as I have quite a few columns I really need to export - however whenever I try to export I get the following error:
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At line:6 char:12
+ $outList | Export-Csv -path D:\scripts\test.xml -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
Code I am using displayed below. Any help much appreciated.
`Get-AdUser -Filter 'Enabled -eq $True' -Properties Name, PasswordLastSet, PasswordNeverExpires, SamAccountName, accountExpires, Company, Description, cn, distinguishedName, info,lastLogonTimestamp, manager |
Where-Object {
$_.PasswordLastSet -lt (Get-Date).AddDays(-365)
} |
Format-Table Name, SamAccountName, PasswordLastSet, PasswordNeverExpires, Company, Description, cn, distinguishedName, info, lastLogonTimestamp, manager
$outList | Export-Csv -path D:\scripts\test.xml -NoTypeInformation
When you run the Get-ADUser command it returns certain properties by default - you only need to specify the non-standard properties that you require. In your case there is no need to specify name and SamAccountName.
$outList = Get-AdUser -Filter 'Enabled -eq $True' -Properties PasswordLastSet, PasswordNeverExpires, accountExpires, Company, Description, cn, distinguishedName, info,lastLogonTimestamp, manager | Where-Object { $_.PasswordLastSet -lt (Get-Date).AddDays(-365)}
The command Format-Table only refers screen output. To select properties from an object, use Select-Object
$outList | Select-Object Name, SamAccountName, PasswordLastSet, PasswordNeverExpires, Company, Description, cn, distinguishedName, info, lastLogonTimestamp, manager | Export-Csv -Path D:\scripts\test.csv -NoTypeInformation
TThe above as a one-line command:
Get-AdUser -Filter 'Enabled -eq $True' -Properties PasswordLastSet, PasswordNeverExpires, accountExpires, Company, Description, cn, distinguishedName, info,lastLogonTimestamp, manager | Where-Object { $_.PasswordLastSet -lt (Get-Date).AddDays(-365)} | Select-Object Name, SamAccountName, PasswordLastSet, PasswordNeverExpires, Company, Description, cn, distinguishedName, info, lastLogonTimestamp, manager | Export-Csv -Path C:\temp\test.csv -NoTypeInformation
I have a list of users in a CSV, but I need to collect the SamAccount attribute from each user by name in the ad.
CSV model
Script
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
I'm a little lost I don't know how to do a foreach using name
I am trying but without success.
Trying to get samaccountname based on Name on csv file.
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
and export to csv file.
Why use Get-ADObject and not Get-ADUser for this? The latter gives you more of the desired properties you need in the CSV.
As aside, it is wasteful to do -Properties * if all you want is a small set of user attributes.
Something like this should work:
Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
As per your comment you need to get some extra attributes of the users listed in the CSV, you can do this:
Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Hope that helps
Using the Get-ADComputer command I am gathering the count of the operating systems for each OU based on when the password was last set. The problem I am facing, is exporting the whole thing into a CSV file.
When I append the (Export-Csv -Path 'c:\blah') it will only take the last command and leave the others in the console.
$ou1 = 'OU=Computers,OU=Name1,DC=domain,DC=com'
$ou2 = 'OU=Computers,OU=Name2,DC=domain,DC=com'
$ou3 = 'OU=Computers,OU=name3,DC=domain,DC=com'
$prop = 'OperatingSystem -Like "Windows 10*"'
Get-ADComputer -SearchBase $ou1 -Filter $prop -Property DistinguishedName, OperatingSystem, pwdLastSet |
Select-Object DistinguishedName, OperatingSystem,
#{Name="pwdLastSet";Expression={[datetime]::FromFileTime($_.pwdLastSet)}},
#{Name="90_Days_Old";Expression={([datetime]::FromFileTime($_.pwdLastSet)).AddDays(90) -le (Get-Date)}}
Get-ADComputer -SearchBase $ou2 -Filter $prop -Property DistinguishedName, OperatingSystem, pwdLastSet |
Select-Object DistinguishedName, OperatingSystem,
#{Name="pwdLastSet";Expression={[datetime]::FromFileTime($_.pwdLastSet)}},
#{Name="90_Days_Old";Expression={([datetime]::FromFileTime($_.pwdLastSet)).AddDays(90) -le (Get-Date)}}
Get-ADComputer -SearchBase $ou3 -Filter $prop -Property DistinguishedName, OperatingSystem, pwdLastSet |
Select-Object DistinguishedName, OperatingSystem,
#{Name="pwdLastSet";Expression={[datetime]::FromFileTime($_.pwdLastSet)}},
#{Name="90_Days_Old";Expression={([datetime]::FromFileTime($_.pwdLastSet)).AddDays(90) -le (Get-Date)}}
My expected result is to have the content of all three commands into a CSV file.
You have a lot of redundant code. Use a loop to avoid that. Also, there's no need to convert the property pwdLastSet (which contains the raw value from the AD attribute) to a DateTime value. The Get-ADComputer cmdlet already does that for you (the name of the property you want is PasswordLastSet).
$ou = 'OU=Computers,OU=Name1,DC=domain,DC=com',
'OU=Computers,OU=Name2,DC=domain,DC=com',
'OU=Computers,OU=name3,DC=domain,DC=com'
$prop = 'OperatingSystem -like "Windows 10*"'
$ou | ForEach-Object {
Get-ADComputer -SearchBase $_ -Filter $prop -Property DistinguishedName, OperatingSystem, PasswordLastSet |
Select-Object DistinguishedName, OperatingSystem, PasswordLastSet,
#{Name="90_Days_Old";Expression={$_.PasswordLastSet.AddDays(90) -le (Get-Date)}}
} | Export-Csv 'C:\path\to\output.csv' -NoType
You can put all your OUs into an array then use a foreach (%) as the SearchBase. This will also allow you to pipe (|) the results to a csv:
#OUs
$OUs = #('OU=Computers,OU=Name1,DC=domain,DC=com','OU=Computers,OU=Name2,DC=domain,DC=com','OU=Computers,OU=name3,DC=domain,DC=com')
$prop = 'OperatingSystem -like "Windows 10*"'
#forach --> | CSV
$OUs | %{Get-ADComputer -Filter $prop -Properties DistinguishedName, OperatingSystem, pwdLastSet -SearchBase $_ | Select-Object DistinguishedName, OperatingSystem,
#{Name="pwdLastSet";Expression={[datetime]::FromFileTime($_.pwdLastSet)}},
#{Name="90_Days_Old";Expression={([datetime]::FromFileTime($_.pwdLastSet)).AddDays(90) -le (Get-Date)}}} | Export-Csv Test123.csv -NoTypeInformation
I have a .csv file with displayName and then the display names of the users.
I am running the following code, but the returned .csv file is blank 0KB. I've spent hours on this and I can't figure out what I'm doing wrong.
(I've even tried switching the displayName to "DisplayName" but that doesn't work)
Get-Content C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$user'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
I just need a simple return of the displayName = samaccountname
If your file is in fact a Csv file with a header for each column,
and displayname is one of them - then use Import-Csv to get the data.
ForEach-Object uses the variable $_ or alternatively $PSItem to assign the currently iterated row of data with the columns as properties.
So change to:
Import-Csv C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
Read this helpful answer on the issue of Get-AdUserand -filter from mklement0
Alright guys, I figured out the problem. The reason the input file was only returning some names is because the some of the names had spaces. For example
Doe, John was written as
Doe , John
with an extra space between the last letter of the last name and comma. To get just the displayName I used the following script:
Import-Csv C:\Scripts\inputfile.txt | ForEach {
Get-ADUser -Filter "displayName -eq '$($_.displayName)'" -Properties Name, SamAccountName |
Select Name,SamAccountName
} | Export-CSV -path C:\output\outputfile.csv -NoTypeInformation
My goal is to make a PS script that creates a CSV file with specefied domain users given name and a timestamp when that user was last modified in unix timestamp format.
I have figured out everything except how to convert the "Modified" AD Attribute value to unix timestamp and how to use that inline in the Select-Object "loop"
I have the following:
get-aduser -filter {name -like test} -property GivenName, Modified | Select-Object GivenName, Modified | Export-CSV -path C:\PS\user.csv
That gives me the following output
Test,2016-02-09 11:48:48
How do i go about doing something like this?:
...| Select-Object GivenName, Modified.convertedToUnixTimestamp() |...
Found it out myself:
get-aduser -filter {name -like "Test*"} -property GivenName, Modified | Select-Object GivenName, #{name="Modified_unix";Expression={Get-Date -date $_.modified -UFormat %s}} | Export-CSV -path C:\PS\user.csv
Using the link in the comments, try something like this to change the property in the pipe:
... | Select-Object GivenName,#{n="unixtimestamp";e={$_.Modified -UFormat ℅s}} |...