在 Powershell 中一次设置多个属性

发布于 2024-11-09 11:22:43 字数 232 浏览 0 评论 0原文

有没有比这更短的方法可以在 Powershell 中通过一个命令将多个属性设置为相同的值?

示例:

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

我只是好奇是否有办法缩短此语法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-11-16 11:22:43
"CreationTime","LastWriteTime","LastAccessTime" |% {(gi test.txt).$_ = (get-date)}
"CreationTime","LastWriteTime","LastAccessTime" |% {(gi test.txt).$_ = (get-date)}
醉酒的小男人 2024-11-16 11:22:43

我使用了 Mjolinor 答案的稍微修改版本来解决刚刚从远程源下载的文件日期不正确的问题。我修改了代码,使其更清晰易懂,以防将来我必须返回(将简写更改为完整的命令名称)。

# Correct Access/Create/Write times on transferred files
ForEach( $File in $TransferList ) {
    @("CreationTime","LastAccessTime","LastWriteTime") | ForEach {
        $(Get-Item $File.Name).$_ = $File.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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文