We help IT Professionals succeed at work.

Read from Text File and Replace String using Powershell

jskfan
jskfan used Ask the Experts™
on
Read from Text File and Replace String using Powershell

For now , I have this command below that works fine when I run it against one remote computer named Windows7 and Replacing one string "username" with "white"
but when I have many computers, then it  does not help

((Get-Content -Path '\\Windows7\c$\test.txt' -Raw) -replace ' username', ' white') | Set-Content -Path '\\Windows7\C$\test.txt'

Open in new window


I will have to put in a separate  Text file like this:

Windows7    White
PCNT34    JDOck
FRANKIST  Ballon
...

Open in new window


the first column is the remote computer name and the second column is the string that will replace the second username.

As I stated , the command I pasted above works just fine, it needs to read from a separate text file so that the command will apply to several computers listed on the text file.

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Input expects a csv with two columns, ComputerName and NewUsername
ComputerName,NewUsername
Windows7,White
PCNT34,JDOck
FRANKIST,Ballon

Open in new window

## csv file with machines and user names to process:
$csv = '.\replace.csv'
## Absolute local file path on the clients:
$filePath = 'C:\Temp\test.txt'

$pattern = '(-username\s+)\S+'
Import-Csv -Path $csv | ForEach-Object {
	$computer = $_.ComputerName.Trim()
	$newUser = $_.NewUsername.Trim()
	$fileUNC = "\\$($computer)\$($filePath.Replace(':', '$'))"
	Write-Host "Processing $($computer), new user '$($newUser)' ..." -NoNewline
	Try {
		$content = Get-Content -Path $fileUNC -ErrorAction Stop
		$content -replace $pattern, "`$1$($newUser)" | Set-Content -Path $fileUNC -Force -ErrorAction Stop
		Write-Host " OK" -ForegroundColor Green
	} Catch {
		Write-Host " FAILED: $($_.Exception.Message)" -ForegroundColor Red
	}
}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
You can't use Excel's binary files.
Just save it an Excel as file type "Csv".

Author

Commented:
I created it in Notepad and saved it as .CSV

ComputerName  NewUserName
Windows7      White
W12           Brown

Open in new window


when I run it I get these errors:
You cannot call a method on a null-valued expression.
At C:\Scripts\changestring.ps1:11 char:2
+     $computer = $_.ComputerName.Trim()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Scripts\changestring.ps1:12 char:2
+     $newUser = $_.NewUsername.Trim()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Processing , new user '' ... FAILED: Cannot find path '\\\C$\test.txt' because it does not exist.
You cannot call a method on a null-valued expression.
At C:\Scripts\changestring.ps1:11 char:2
+     $computer = $_.ComputerName.Trim()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Scripts\changestring.ps1:12 char:2
+     $newUser = $_.NewUsername.Trim()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Open the csv in a text editor and check what Excel used as delimiter when saving; it's probably not a comma.
Then add the -Delimiter argument to Import-Csv i line 7
Import-Csv -Path $csv  -Delimiter "<Delimiter here>" | ForEach-Object {

Open in new window

Author

Commented:
Wow !! working now.
I just created text file  with Notepad that includes:

ComputerName,NewUserName
Windows7,White
W12,Brown

Open in new window


Saved it as .CSV

I ran the script and it did the change  on remote computers.

Thank you very much for your Help.

Author

Commented:
Excellent!!
Thank you