Home > Software design >  how to parse a value to variable using powershell from data in table format
how to parse a value to variable using powershell from data in table format

Time:08-24

I have command $Var1=Get-ChildItem -Attributes !Directory Statement_*.pdf | Sort-Object -Descending -Property LastWriteTime | select -First 1|Select-Object "Name" which gives me output in below format

Name
----
Statement_2022MTH05_750571314.pdf

I only want to store Statement_2022MTH05_750571314.pdf file name in a variable, so that when I fire command Write-Output $Var1 then it should print Statement_2022MTH05_750571314.pdf only. Thanks in advance.

CodePudding user response:

You can use -ExpandProperty Name which will Specifies a property to select, and indicates that an attempt should be made to expand that property. So,

Select-Object -First 1 | Select-Object "Name"

can be replaced with

Select-Object -First 1 -ExpandProperty Name
  • Related