Avatar of jskfan
jskfanFlag for Cyprus

asked on 

Read from Text File and Replace String using Powershell

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
PowershellScripting Languages

Avatar of undefined
Last Comment
jskfan
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of oBdA
oBdA

You can't use Excel's binary files.
Just save it an Excel as file type "Csv".
Avatar of jskfan
jskfan
Flag of Cyprus image

ASKER

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

Avatar of oBdA
oBdA

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

Avatar of jskfan
jskfan
Flag of Cyprus image

ASKER

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.
Avatar of jskfan
jskfan
Flag of Cyprus image

ASKER

Excellent!!
Thank you
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo