0

I would like to loop through a CSV File and use the enties to create a .txt File.

CSV File would look like something like this:

Column1   Column2   Column3       
-------   -------   -------       
Systemname1  User1 PasswordtEX#
Systemname2  User2 Password4k2P
Systemname3  User3 Password3Uch
Systemname4  User4 PasswordY0}(
Systemname5  User5 PasswordDhE"
Systemname6  User6 Password1TOs

So the code I had in mind would look like that:

$data = Import-Csv 'C:\Path.csv' -Delimiter ";"

ForEach ( $Systemname in $data) {
"Systemname is $Systemname.Column1 and the User I'm going to use it named $User.Column2 with the Password $Password.Column3" Add-content | out-file -filepath C:\path.txt -Append
}

And the .txt File would look like that

Systemname is Systemname1 and the User I'm going to use is named User1 with the Password PasswordtEX#
Systemname is Systemname2 and the User I'm going to use is named User2 with the Password Password4k2P
Systemname is Systemname3 and the User I'm going to use is named User3 with the Password Password3Uch
Systemname is Systemname4 and the User I'm going to use is named User4 with the Password PasswordY0}(
.... And so on

Am I totally off track? Would appreciate any help! Thanks in regards!

2
  • "Am I totally off track?" - why don't you test your current code and find out for yourself? :-) Commented May 11, 2021 at 8:05
  • Cool! Feel free to answer your own question :-) Commented May 11, 2021 at 8:57

2 Answers 2

1

I actually found a solution myself,

ForEach ( $Systemname in $data) {
$Hostname = $Systemname.Column1
$Benutzername = $Systemname.Column2
$pw = $Systemname.Column3

ForEach ( $Systemname in $data) {
"Systemname is $Hostname and the User I'm going to use it named $Benutzername with the Password $pw" | out-file -filepath 'C:\path.txt' -Append
}

I included variables in the foreach loop like that.

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

Comments

0

You're really getting the hang of it I see.

Here's some more alternatives to do that without defining extra variables:

  1. Using Subexpression
$data = Import-Csv 'C:\Path.csv' -Delimiter ";"
foreach ($item in $data) {
    "Systemname is $($item.Column1) and the User I'm going to use it named $($item.Column2) with the Password $($item.Column3)" | 
    Out-File -FilePath 'C:\path.txt' -Append
}
  1. Using the -f Format operator
$data = Import-Csv 'C:\Path.csv' -Delimiter ";"
foreach ($item in $data) {
    "Systemname is {0} and the User I'm going to use it named {1} with the Password {2}" -f $item.Column1, $item.Column2, $item.Column3 | 
    Out-File -FilePath 'C:\path.txt' -Append
}
  1. Using a ForEach-Object loop and the $_ Automatic variable
    (here combined with the -f Format operator, piping the result to file straight away)
Import-Csv 'C:\Path.csv' -Delimiter ";" | ForEach-Object {
    "Systemname is {0} and the User I'm going to use it named {1} with the Password {2}" -f $_.Column1, $_.Column2, $_.Column3 | 
} | Out-File -FilePath 'C:\path.txt' -Append

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.