Installing multiple properties at once in Powershell

Is there a shorter way to set a few properties to the same values ​​in a Powershell command than that?

Example:

(gi "c:\test.txt").LastWriteTime = (gi "c:\test.txt").LastAccessTime = (gi "c:\test.txt").CreationTime = Get-date 

I'm just wondering, is there a way to shorten this syntax.

+4
source share
2 answers
 "CreationTime","LastWriteTime","LastAccessTime" |% {(gi test.txt).$_ = (get-date)} 
+7
source

I used a slightly modified version of Mjolinor answer to solve the problem with the incorrect date in the files that were just loaded from a remote source. I changed the code to make it more intuitive to understand, if I have to go back in the future (short changed hands on the full names of the teams).

 # Correct Access/Create/Write times on transferred files ForEach( $File in $TransferList ) { @("CreationTime","LastAccessTime","LastWriteTime") | ForEach { $(Get-Item $File.Name).$_ = $File.Date } } ", "LastWriteTime") | # Correct Access/Create/Write times on transferred files ForEach( $File in $TransferList ) { @("CreationTime","LastAccessTime","LastWriteTime") | ForEach { $(Get-Item $File.Name).$_ = $File.Date } } . $ _ = $ File.Date # Correct Access/Create/Write times on transferred files ForEach( $File in $TransferList ) { @("CreationTime","LastAccessTime","LastWriteTime") | ForEach { $(Get-Item $File.Name).$_ = $File.Date } } 
0
source

All Articles