0

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}
TudorIftimie
  • 1,050
  • 11
  • 21
RIC
  • 15
  • 4

1 Answers1

0

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

Farbkreis
  • 604
  • 3
  • 12
  • Thank you so much for your reply. I ran that as it is, it ran without showing any errors, but didn't change the file name. Any advise? – RIC Oct 19 '21 at 21:39
  • Any advise please? – RIC Oct 20 '21 at 01:44
  • I changed C:\users*\ to C:\users\ as I assumed that the folders are in subfolders - is my assumption correct or not? – Farbkreis Oct 20 '21 at 05:38
  • You are a legend, thank you so much. Below worked. Just put \\*\ after 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' } } – RIC Oct 20 '21 at 12:01
  • Hey @Farbkeis, how can I skip file copy if the same file exists already? my current code to copy the file is this - $Source = '\\ggfglenser04\h\AppsData\SAPUILandscape.xml' $Destination = 'C:\users\*\AppData\Roaming\SAP\Common\' Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force} – RIC Oct 26 '21 at 00:23
  • 1
    https://stackoverflow.com/questions/69716214/skip-file-copy-if-target-exist-powershell – RIC Oct 26 '21 at 00:42