Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

How to remove the number of days from "4/22/2019 5:00:30 AM (180 days ago)" with PowerShell?

Hi,

I need to import users info from csv. The problem is the date format in lastlogon header from the csv, like:

4/22/2019 5:00:30 AM (180 days ago)

When I will do:

$users | Select SamAccountName,Lastlogon


How can I keep just the date  and not the number of days, like: 4/22/2019 5:00:30 AM?
Avatar of aikimark
aikimark
Flag of United States of America image

something like this?
$a = "4/22/2019 5:00:30 AM (180 days ago)"
[datetime]$a.split('(')[0]

Open in new window

or this
[datetime]($a -split ' \(')[0]

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

The csv is produced by an application. That is why I have no choice to make some changes with PowerShell because the date format can’t be read.
Avatar of oBdA
oBdA

OK; then the import with a Select-Object and a calculated property as described above is the way to go.
Hi,

This work for me

Get-ADUser -Filter * -Properties SamAccountName, Lastlogondate | Select SamAccountName, Lastlogondate | export-csv C:\Temp\test.csv

The output date looks like this 9/13/2019  8:30:41 AM

The you can make to all format you need with Excel