【问题标题】:Rename a file in Roaming folder for each user in a computer using powershell使用powershell为计算机中的每个用户重命名漫游文件夹中的文件
【发布时间】:2021-12-06 18:56:29
【问题描述】:

我正在尝试为 PC 上的每个用户配置文件重命名 \appdata\Roaming\ 中的文件。 我正在尝试这个命令,但没有工作。 我究竟做错了什么?请帮忙。

$old = 'C:\users\*\AppData\Roaming\SAP\Common\test.txt'
$rename = 'C:\users\*\AppData\Roaming\SAP\Common\test-old.txt'
Get-ChildItem $rename | ForEach-Object {Rename-Item -Path $old -NewName $_ -Force}

【问题讨论】:

    标签: powershell foreach rename user-profile appdata


    【解决方案1】:

    我做错了什么?几乎所有东西;-)。

    请看看这是否满足您的需要:

    $folders = get-childitem 'C:\users\' -directory
    
    foreach ($folder in $folders) {
        $file = Join-Path $folder -ChildPath 'AppData\Roaming\SAP\Common\test.txt'
        if (Test-Path $file) {
            rename-item $file -NewName 'test-old.txt'
        }
    }
    
    

    【讨论】:

    • 非常感谢您的回复。我按原样运行它,它运行时没有显示任何错误,但没有更改文件名。有什么建议吗?
    • 有什么建议吗?
    • 我将 C:\users*\ 更改为 C:\users\,因为我假设文件夹位于子文件夹中 - 我的假设是否正确?
    • 你是个传奇,非常感谢。下面工作。只需将 \*\ 放在 C:\users $folders = get-childitem 'C:\users*\' -directory foreach ($folder in $folders) { $file = Join-Path $folder -ChildPath 'AppData\Roaming\ SAP\Common\test.txt' if (Test-Path $file) { rename-item $file -NewName 'test-old.txt' } }
    最近更新 更多