Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split CustomObject in function?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading