0

I have one file say abc.txt and the content of the file is like this :

         user1
         user2
         user3
         etc

I want to store the content of the file in a variable like this : user1;user2;user3;user4...

I have used below script in powershell lke this :

   $variabl1=([io.file]::readAllText("E:\abc1.txt") -replace "`n") 

but I am not getting anything when i am using below cmd :

            write-host $variabl1

I want the value to store in the variable please help.

1 Answer 1

1

It's not working because you're not telling -replace what to put in place of \n. Try:

-replace "`n",";"

OR

Use Get-Content to read the contents into a collection of lines; you can then join that collection (array) with the -join operator into a single string.

$variabl1 = (Get-content -path "e:\abc1.txt") -join ";"
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your suggestion but the last ; i m missing...like the output should be user1;user2;user3;user4; if there are 4 entries in the file upto user4
Then append a semicolon at the end of the string. You'd have to do the same thing if your input file didn't have a trailing newline.

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.