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