Sunday, 30 August 2020

powershell 21 change file content on remote computer

original remote file

remote file after update

PS C:\Windows\System32> $contents = invoke-command -session $session -scriptblock {
>>     $fileName = "C:\Users\zchen\Desktop\abc.txt"
>>     (Get-Content -Path $fileName) |
>>         ForEach-Object {$_ -Replace 'line2', $Env:COMPUTERNAME} |
>>             Set-Content -Path $fileName
>>     Get-Content -Path $fileName
>> }

PS C:\Windows\System32> $contents
line1
DESKTOP-K1RH4M9
line3

reference:

No comments:

Post a Comment