0

Is there a way to read a text file D:\SetCredentials.txt which contains credentials and set the values in variables so that I can use those variables in other script.

File looks like this:

serv|16762646t|6879i0i2|9878p989868

I want to set the value of a variable in my script below is the code we use in batch script but i want it in powershell script

For /F "tokens=1-4 delims=|" %%A in (SetCredentials.txt) do (

set username=%%A
set password=%%B
set servername=%%C
set key=%%D

Below is something i wrote, it is just reads and splits the text file -

$credInFile = Get-Content -path "D:\SetCredentials.txt"
$credInFileSplit = credInFile.Split("|")
foreach($i in $credInFileSplit){
 echo $i
}
5
  • 1
    In your example - change $credInFileSplit = $credInFile.Split("|") then look at each element of $credInFileSplit e.g. $credInFileSplit[0] , $credInFileSplit[1] etc Commented Jan 24, 2023 at 12:47
  • 2
    powershell also supports multi assignment i.e.: $foo, $bar, $baz = 'foo|bar|baz'.Split('|') Commented Jan 24, 2023 at 12:51
  • Hi @Scepticalist ,How can i set the variables like $username=serv, , $password=16762646t from a text file as shown above . Commented Jan 24, 2023 at 12:59
  • Hello @SantiagoSquarzon, , how can i read the txt file then? Commented Jan 24, 2023 at 13:06
  • 2
    Once you know which field has what data in it, just use Import-Csv -Path 'X:\yourfile.txt' -Header 'Server','Password','UserName','Key' -Delimiter '|'. Since the fields are unclear to me, that header order may be wrong, only you can tell as it is now.. Commented Jan 24, 2023 at 13:08

2 Answers 2

1

Import-CSV can handle this easily. Assuming that the format is

username|password|servername|key

you can use

$Credential = Import-CSV -Path "D:\SetCredentials.txt" -Header "Username","Password","ServerName","Key" -Delimiter "|"

You now have a PSCustomObject with four members Username, Password, ServerName, and Key, which can be passed as parameters (either as the object entire, or as individual strings, depending on the receiving code) to other cmdlets, advanced functions, or scripts.

(The PSCustomObject is called $Credential, and the fields (or members) are called $Credential.UserName, $Credential.Password, $Credential.ServerName, and $Credential.Key. You can use the individual fields as though they were separate variables.)

(If the order of the fields is different in the file, rearrange the entries in the -Header parameter to match the actuality)

In general, it's a bad idea to try to replicate a batch file's process identically in PowerShell; there are enough differences that you should think carefully about the intent of the process, and take advantage of the capabilities that PowerShell offers that are not available in batch.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Jeff Zeitlin. Thank you ! can you please advise me from where can i learn powersehll
@BenMartin2 - There are a large number of PowerShell books of all levels out there; you would have to find the ones that suit you best. I have found that in general, the ... for Dummies series of books are well-written and present the material clearly; Manning Publications' Learn ... in a Month of Lunches series is also well-written and clear, but in a different style.
Hi @Jeff , Thanks though but the above code isn't working or I'm not understanding cause the scripting is new to me. Below code from scepticalist is working as expected
@BenMartin2 - If you're new enough to PowerShell that you're not clear on the concept of objects and members, I'd strongly suggest that you find some good beginners' books on PowerShell - I recommend Manning's Learn PowerShell in a Month of Lunches, followed by Learn PowerShell Scripting in a Month of Lunches and Learn PowerShell Toolmaking in a Month of Lunches. There will be a lot of overlap in those three books, but together they're a good introduction to PowerShell.
0

You can assign variables directly from array elements:

$credInFile = Get-Content -path "D:\SetCredentials.txt"
$credInFileSplit = $credInFile.Split("|")

$username=$credInFileSplit[0]
$password=$credInFileSplit[1]
$servername=$credInFileSplit[2]
$key=$credInFileSplit[3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.