开发者

Setting multiple properties at once in Powershell

开发者 https://www.devze.com 2023-03-08 05:10 出处:网络
Is there a shorter way to set multiple properties to the 开发者_运维问答same value in Powershell in one command than this?

Is there a shorter way to set multiple properties to the 开发者_运维问答same value in Powershell in one command than this?

Example:

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

I'm just curious if there is a way to shorten this syntax.


"CreationTime","LastWriteTime","LastAccessTime" |% {(gi test.txt).$_ = (get-date)}


I've used a slightly modified version of Mjolinor's answer to solve a problem I had of incorrect date on files that had just been downloaded from a remote source. I modified the code to make it cleaner to understand in case I have to come back to in the future (changed the short hand to full command names).

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号