Sohail Arif
Sohail Arif

Reputation: 84

Formatting Dates in PowerShell

I am currently trying to convert a date from the one displayed in regedit to a readable datetime format. But I do not know how to do this, I'm working with the following:

.GetValue('InstallDate')

And in the .csv file, it display it as this: 20150914

How would I go about converting that into a readable date?

Upvotes: 0

Views: 721

Answers (3)

cloudbase
cloudbase

Reputation: 23

I'm not sure why you down voted the other answer because he is right on the money with [Datetime]::ParseExact you will have to deal with the null values though

    $Regbase = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
    foreach ($entry in $regBase)
    {
    $date = (Get-ItemProperty HKLM:\$entry | select installdate).installdate
        try
        {
            [DateTime]::ParseExact($date, "yyyyMMdd", [CultureInfo]::InvariantCulture) 
        }
        catch [exception] 
        {
            "Date Value: $date" 
        }
    }

Upvotes: 1

qxg
qxg

Reputation: 7036

PowerShell Date is just .NET DateTime. Check DateTime.ParseExact.

[DateTime]::ParseExact("20151010", "yyyyMMdd", [CultureInfo]::InvariantCulture)

Upvotes: 0

CB.
CB.

Reputation: 60918

try

[datetime]::Parseexact("20150914","yyyyMMdd", $null  )

Upvotes: 1

Related Questions