0

I would like to add a line of ---- to the output I am writing from PowerShell. There are multiple lines being added to the text file similar to the code I have pasted here and I would like to just have this ---- added as a line before the output to quickly differentiate the info when looking at it.

"New Starter- " +$FirstName + " " +  $Lastname + ", " + "User Name- " + $SAMAccountLower + ", " + "Temp Password- " +$TmpPass | Set-Content "c:\temp\${SAMAccountLower}_login.txt"

Thanks.

3 Answers 3

1

If you do not want to overwrite the contents of an existing file, you can use Add-Content. If you want to overwrite a file or start a new file, then change Add-Content to Set-Content. I tend to favor using the format operator (-f) for string formatting/building, which can protect from certain issues with + concatenation.

'----' | Add-Content "c:\temp\${SAMAccountLower}_login.txt"
"New Starter- {0} {1}, User Name- {2}, Temp Password- {3}" -f $FirstName,$LastName,$SamAccountLower,$TmpPass |
    Add-Content "c:\temp\${SAMAccountLower}_login.txt"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, yeh the set-content has made a difference to the overwriting aspect of this also. Cheers for that.
0
"----- " + "New Starter- " +$FirstName + " " +  $Lastname + ", " + "User Name- " + $SAMAccountLower + ", " + "Temp Password- " +$TmpPass | Set-Content "c:\temp\${SAMAccountLower}_login.txt"

You mean like this^ or like this?

"-----`n" + "New Starter- " +$FirstName + " " +  $Lastname + ", " + "User Name- " + $SAMAccountLower + ", " + "Temp Password- " +$TmpPass | Set-Content "c:\temp\${SAMAccountLower}_login.txt"

3 Comments

I wanted to have a line of ---- then the output, but I just read up about using "@ $UserInfo = @" --- New Starter Name: $FirstName $Lastname Username: $SAMAccountLower Temp Password: $tmpPass --- "@ '$UserInfo | Set-Content "c:\temp\${SAMAccountLower}_login.txt"
sorry that reply is really messy.
I am assuming you are getting the data from Get-ADUser command. Simply displaying the return object itself would look better. Since they are objects, you can add the Temp Password as a property using add-member cmdlet if you wish. But whatever works for you. There will always be multiple ways of handling it. personally, I would always like to handle it as a object. That way, it will always be a unit.
0

From what i understood, you want to write your line proceeded by --- like this:

< something written here >
-------------------------- 
< Your line here >

Try this:

$text = "New Starter- " +$FirstName + " " +  $Lastname + ", " + "User Name- " + $SAMAccountLower + ", " + "Temp Password- " +$TmpPass;
$seperator = '-'*$text.Length + "`n";
$seperator + $text | Set-Content "c:\temp\${SAMAccountLower}_login.txt";

NOTE

I think Set_Content removes the previous content of the file and write the new text, i don't know if this is your case.

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.