r/PowerShell•
cnuthead

Converting Australian date to freedom date in PS

Solved

Hey all,

So I'm sure this is a simple one, however, it has me stumped.

I am creating a powershell script to set account expiry using Set-ADAccountExpiration. the date that I am passing over is in australian date format (dd/MM/yy), however, PS does not like it.

How am I able to convert it to MM/dd/yy so it works with PowerShell?

Here is my script

AccountName = "test"

$ExpiryDate = "27/03/2018"

$ExpiryTime = "5:30 PM"

##Set-ADAccountExpiration -Identity $AccountName -DateTime "$ExpiryDate $ExpiryTime"

I get the error: Set-ADAccountExpiration : Cannot bind parameter 'DateTime'. Cannot convert value "27/03/2018 5:30 PM" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

I have tested $ExpiryDate = $Date.ToString("MMddyyyy"), however, get this error Cannot find an overload for "toString" and the argument count: "1". At C:\Scripts\Set Account Expiry.ps1:4 char:1

Any ideas?

Posted Mar 13, 2018, 10:09 AM UTC

69
Loading comments…