Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

How to format InstallDate property used in Select-Object?


The InstallDate property used in the below code -

$InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, 
InstallDate  | Format-Table –AutoSize

$logDisplayName = $InstalledProgram.DisplayName 
$logPublisher = $InstalledProgram.Publisher
$logVersion = $InstalledProgram.DisplayVersion
$logInstallDate= $InstalledProgram.InstallDate

It displays the date of an installed application in this format 20170921 but i want it to be displayed in 21/09/2017 i.e DD/MM/YYYY format. How can i format it?

Below is the Entire Code which is used to copy the output to log file:-

Clear-Host
$scriptPath = $PSScriptRoot
$logFilePath= Join-path $scriptPath "POCTestResults.log"

# If log file exists, then clear its contents 
if (Test-Path $logFilePath)
{
    clear-content -Path $logFilePath
} 

# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
add-content -Path $logFilePath -Value $log -Force 
add-content -Path $logFilePath -Value $logString -Force 
add-content -Path $logFilePath -Value "`n" -Force



# Validate ADD/Remove Program list
# FUNCTION DEFINITIONS
function Log-InstalledProgram($InstalledProgram, $LogFilePath)
{
    #$InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate  | 
    #Format-Table –AutoSize
    $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, @{Name="InstallDate"; Expression={([datetime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).toshortdatestring()}}  | Format-Table –AutoSize

$logDisplayName = $InstalledProgram.DisplayName 
$logPublisher = $InstalledProgram.Publisher
$logVersion = $InstalledProgram.DisplayVersion
$logInstallDate= $InstalledProgram.InstallDate

    add-content -Path $LogFilePath -Value "Product Name: $logDisplayName" -Force   
    add-content -Path $LogFilePath -Value "Publisher: $logPublisher" -Force  
    add-content -Path $LogFilePath -Value "Version: $logVersion" -Force  
    add-content -Path $LogFilePath -Value "InstallDate: $logInstallDate" -Force  
    add-content -Path $LogFilePath -Value "`n" -Force
}

add-content -Path $logFilePath -Value "`n" -Force
add-content -Path $logFilePath -Value "Add/Remove Programs :" -Force
add-content -Path $logFilePath -Value "`n" -Force

$InstalledPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

foreach ($InstalledProgram in $InstalledPrograms )
{
    foreach ($displayName in "IntelliMatch Operational Control","intelliSuite Management Studio", "SunGard System Analyzer", "STeP")
    {
        if(($InstalledProgram.DisplayName -ne $Null) -and ($InstalledProgram.DisplayName.Contains($displayName)))
        {
            Log-InstalledProgram $InstalledProgram $logFilePath
        }
    }
}

This is script i have used.


Solution

  • Use [datetime]::ParseExact() method and calculated properties to retrieve a datetime format in the Select-Object and format it in a short date string with .toshortdatestring() :

    $InstalledProgram |  Select-Object DisplayName, DisplayVersion, Publisher, @{Name="InstallDate"; Expression={([datetime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).toshortdatestring()}}  | Format-Table –AutoSize
    
    $logDisplayName = $InstalledProgram.DisplayName 
    $logPublisher = $InstalledProgram.Publisher
    $logVersion = $InstalledProgram.DisplayVersion
    $logInstallDate= $InstalledProgram.InstallDate