powershellforeachrenameuser-profileappdata

Rename a file in Roaming folder for each user in a computer using powershell


I am trying to rename a file in \appdata\Roaming\ for each user profile on a pc. The command I am trying this, but not working. What am I doing wrong? Please help.

$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}

Solution

  • What am I doing wrong? Almost everything ;-).

    Please take a look if this does what you need:

    $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'
        }
    }