Convert Date to UnixTimestamp in a "Select-Object" loop. PowerShell - powershell

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}} |...

Related

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 ";"
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.

Trying to input a specific user list for get-aduser

I'm trying to use a list of usernames to perform a simple get-aduser command. It works fine for a single user, but I can't input a file to perform this for a list.
This command works fine for a single user:
get-aduser -identity myusername -properties passwordlastset, passwordneverexpires |
sort name | ft Name, passwordlastset, Passwordneverexpires | out-file c:\PS\Output.txt
This works fine, but rather than use -filter * for all AD or identity pointing to a file, I am completely lost. I have tried doing a get-content and link to a file but I'm just getting into a pickle.
If I have a text file with a list of usernames in, how do I run the above command against that single text file list, rather than all of AD?
As a side query, is there a way that I can perform the above command, but for a specific OU?
If you have a list that isn't an object, either import it to an object or iterate over the values
Try something like:
$Userlist = Get-Content -path 'c:\temp\test.txt'
$Results = $Userlist | ForEach-Object {
Get-aduser -identity $_ -properties passwordlastset, passwordneverexpires
}
$Results | sort name | ft Name, passwordlastset, Passwordneverexpires | out-file c:\PS\Output.txt
This will work as long as you supply valid SamAccountNames in your list
I would do it this way. You can pipe in identity byvalue. You can import the csv later and get objects back.
get-content userlist.txt |
Get-aduser -properties passwordlastset, passwordneverexpires |
sort name |
select Name, passwordlastset, Passwordneverexpires |
export-csv users.csv
# searchbase example
get-aduser -filter 'name -like "j*"' -SearchBase 'OU=People,DC=stackoverflow,DC=com'

Get Samaccountname from display name into .csv

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

List of users based on Description in Active Directory using Powershell

I'm basically new to PowerShell. May I ask what do I need to do in Get-ADUser -filter.. to pull out users that have the same description?
Get-ADUser -Filter * -Properties Description | Select Name,Description | Sort Description
You could add another pipe to Format-List or Format-Table or export with Export-CSV or Export-CliXML
You could use the Group-Object cmdlet to group object together with the same description.
Get-ADUser -Filter * -Properties Description |
Select-Object Name,Description |
Group-Object Description |
Where-Object {$_.count -gt 1}

Export Get-ADComputer to CSV [duplicate]

This question already has answers here:
How to get an object's property's value by property name?
(6 answers)
Closed 5 years ago.
I am working on a script which takes Hostnames from a CSV, Files them against Get-ADComputer and then saves certain Objects in certain columns of the original CSV.
While the solution seems like a basic Task (code below), my problem is, that the Output of Get-ADComputer always (you can see I played around with Out-String but also tried other formatting options) contains a lot of NewLine characters or other formatting issues which make the CSV confusing.
Clear-Host
Import-Module ActiveDirectory
$import = Import-Csv 'XXX' -Delimiter ';'
Foreach ($row in $import){
$hostname = $row.HOSTNAME
if($hostname.length -gt 3){
$computer = Get-ADComputer -Filter {Name -like $hostname} -Properties LastLogonDate, LastLogonTimeStamp
$row.AD_LastLogon.ToString() = $computer | Select-Object LastLogonDate | Select-Object -first 1 | FT -HideTableHeaders | Out-String
$row.AD_LLTimestamp = $computer | Select LastLogonTimestamp |Select-Object -first 1 | FT -HideTableHeaders | Out-String
}
}
$import | Export-Csv 'XXX' -Delimiter ';' -NoType
My question now is, if anyone could help with a method to get the bare string result of for example Get-ADComputer's LastLogonDate, without any formatting or headers included.
Thanks in advance!
Use the -ExpandProperty parameter of Select-Object to extract just the parameter you want. You can only specify one parameter to exapand at a time.
$row.AD_LastLogon = $computer | Select-Object -ExpandProperty LastLogonDate -First 1 | Out-String
$row.AD_LLTimestamp = $computer | Select-Object -ExpandProperty LastLogonTimestamp -First 1
I don't believe the -First 1 should be necessary. Get-ADComputer shouldn't be finding multiple computers with the same name.
Also, you shouldn't need to retrieve both LastLogonDate and LastLogonTimestamp. The former is the same value as the latter, just converted to a DateTime from the irritating NT Time Epoch that LastLogonTimestamp uses. Have you got a system that requires both?
Finally, just a note but this:
$row.AD_LastLogon.ToString() = $computer | [...]
It doesn't make sense. You can't assign a value to a method. I would be surprised if that didn't error or otherwise do nothing at all.