如何格式化 Select-Object 中使用的 InstallDate 属性?

How to format InstallDate property used in Select-Object?

以下代码中使用的 InstallDate 属性 -

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

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

它以这种格式 20170921 显示已安装应用程序的日期,但我希望它以 21/09/2017DD/MM/YYYY 格式显示。我该如何格式化它?

下面是用于将输出复制到日志文件的完整代码:-

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
        }
    }
}

这是我用过的脚本。

使用 [datetime]::ParseExact() 方法和 calculated properties 检索 Select-Object 中的日期时间格式,并使用 .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