将标题行添加到PowerShell的文本输出中

时间:2019-06-25 06:12:33

标签: powershell printing

我想向----写入的输出中添加一行PowerShell。 类似于我在此处粘贴的代码,文本文件中添加了多行,我只想在输出之前在行中添加此----作为行,以便在查看时快速区分信息。

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

谢谢。

3 个答案:

答案 0 :(得分:0)

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

您是说像这样^还是这样?

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

答案 1 :(得分:0)

据我所知,您想以---开头写这行:

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

尝试一下:

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

注意

我认为Set_Content删除了文件的先前内容并写入了新文本,我不知道这是否是您的情况。

答案 2 :(得分:0)

如果您不想覆盖现有文件的内容,则可以使用Add-Content。如果要覆盖文件或启动新文件,请将Add-Content更改为Set-Content。我倾向于使用格式运算符(-f)进行字符串格式化/构建,该操作符可以防止由于+串联而出现的某些问题。

'----' | 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"