Search code examples
powershellreplacestr-replace

Change the contents of a rdp-file with PowerShell


I have a directory C:\RDP LINKS\ in which you find a whole bunch of folders with rdp shortcuts.

The problem is, is that the default name in all of the shortcuts has been changed recently (first it was administrator@testdomain.local and now it's administrator@test2domain.local.

i want to change the contents of the rdp links with Powershell.

If you open the rdp shortcut with notepad, you can see the properties and the default user name:

enter image description here

I tried this script:

$configFiles = Get-ChildItem "C:\RDP LINKS\" *.rdp -rec
foreach ($file in $configFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace "administrator@testdomain.local", "administrator@test2domain.local" } |
    Set-Content $file.PSPath
}

The script runs but the name doesn't change. What am i doing wrong?


Solution

  • try this instead (use fullname property instead of pspath) :

     ls "c:\rdp links\*.rdp" -recurse | %{
         (gc $_ ) -replace "administrator@testdomain.local", "administrator@test2domain.local" |
         set-content $_.FullName -force
    }