Home > Back-end >  How to split CustomObject in function?
How to split CustomObject in function?

Time:04-05

I have problem with this code. I have function to scan apps 32-64 in PC and then export it to excel. Problem is, that "'Datum_Instalace' = $_.InstallDate" is string and I don't know how to parse it. For example, application "Microsoft Edge" has 'Datum_Instalace' 20181128 and I want to change it into 2018.11.28 or even better 28.11.2018 with powershell.

[PSCustomObject]@{

'Nazev_PC' = hostname

'Bit' = $b2

'Nazev_Programu' = $_.DisplayName

'Verze' = $_.DisplayVersion

'Datum_Instalace' = $_.InstallDate

'Vydavatel' = $_.Publisher

}

How it looks like in Excel

CodePudding user response:

Use [datetime]::ParseExact() to parse the InstallDate string after which you can re-format it:

[PSCustomObject]@{
  'Nazev_PC' = hostname
  'Bit' = $b2
  'Nazev_Programu' = $_.DisplayName
  'Verze' = $_.DisplayVersion
  'Datum_Instalace' = try{ [datetime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null).ToString('dd.MM.yyyy') } catch { '' }
  'Vydavatel' = $_.Publisher
}

Now PowerShell will attempt to parse the input string with the format yyyyMMdd and subsequently output a string in the format dd.MM.yyyy. If the conversion fails, we simply output an empty string.

  • Related