none
how to convert a string value to date RRS feed

  • Question

  • Dear All,

    I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.

    but when i try to convert the variable to date value, I am getting the error as below.

    Please help me......

    PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
    PS C:\script> $passwordSetDate

    PasswordLastSet
    ---------------
    7/15/2014 8:17:24 PM


    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    Cannot find an overload for "ParseExact" and the argument count: "3".
    At line:1 char:1
    + $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)

    Monday, July 21, 2014 2:55 PM

Answers

  • One simple way to return a DateTime as a formatted string is by using the -f operator. For example:


    PS C:\> $passwordSetDate = get-aduser user1 -properties PasswordLastSet | select-object -expandproperty PasswordLastSet
    PS C:\> $passwordSetDate
    Monday, July 21, 2014 8:15:04 AM
    PS C:\> "{0:MM/dd/yyyy HH:mm:ss}" -f $passwordSetDate
    07/21/2014 08:15:04
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by sarmakumar Monday, July 21, 2014 6:09 PM
    Monday, July 21, 2014 3:00 PM
    Moderator

All replies

  • One simple way to return a DateTime as a formatted string is by using the -f operator. For example:


    PS C:\> $passwordSetDate = get-aduser user1 -properties PasswordLastSet | select-object -expandproperty PasswordLastSet
    PS C:\> $passwordSetDate
    Monday, July 21, 2014 8:15:04 AM
    PS C:\> "{0:MM/dd/yyyy HH:mm:ss}" -f $passwordSetDate
    07/21/2014 08:15:04
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by sarmakumar Monday, July 21, 2014 6:09 PM
    Monday, July 21, 2014 3:00 PM
    Moderator
  • Return only the datetime value using dotted notation.

    $passwordSetDate = (Get-ADUser user1 -properties * | select PasswordLastSet).PasswordLastSet

    To verify you have a datetime value, pipe the variable to Get-Member.

    $passwordSetDate | Get-Member | Select-Object TypeName -Unique


    Monday, July 21, 2014 3:00 PM
  • The simple answer may be this:

    $passwordSetDate=(get-aduser user1 -properties PasswordLastSet).PasswordLastSet

    Or this:

    (get-aduser user1 -properties PasswordLastSet).PasswordLastSet.ToString('MM/dd/yyyy HH:mm:ss')

    Note that the formatter characters are case sensitive.


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, July 21, 2014 3:23 PM
    Monday, July 21, 2014 3:19 PM
  • Dear All,

    I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.

    but when i try to convert the variable to date value, I am getting the error as below.

    Please help me......

    PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
    PS C:\script> $passwordSetDate

    PasswordLastSet
    ---------------
    7/15/2014 8:17:24 PM


    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    Cannot find an overload for "ParseExact" and the argument count: "3".
    At line:1 char:1
    + $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)

    In your post you ask how to convert a string value to a date.  The value returned from the Get-AdUser is already a date.  It does not need to be converted.

    Bill has sshown one way to convert a date to a string and there are other methods.  You need to clarify your question.

    If you are really trying ot convert strings to dates then you can start with this:

    [datetime]'01/21/1965 13:33:23'

    Most date strings aer autodetected by the class.


    ¯\_(ツ)_/¯

    Monday, July 21, 2014 3:37 PM