PowerShell의 명령 출력에 줄 바꿈을 추가하려면 어떻게해야합니까?
PowerShell을 사용하여 다음 코드를 실행하여 레지스트리에서 프로그램 추가 / 제거 목록을 가져옵니다.
Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
| ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") } `
| Out-File addrem.txt
목록을 각 프로그램별로 줄 바꿈으로 구분하고 싶습니다. 난 노력 했어:
Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
| ForEach-Object -Process { Write-Output $_.GetValue("DisplayName") `n } `
| out-file test.txt
Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
| ForEach-Object {$_.GetValue("DisplayName") } `
| Write-Host -Separator `n
Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
| ForEach-Object -Process { $_.GetValue("DisplayName") } `
| foreach($_) { echo $_ `n }
그러나 콘솔로 출력 할 때 모든 형식이 이상하고 파일로 출력 할 때 각 줄 뒤에 세 개의 정사각형 문자가 표시됩니다. 나는 노력 Format-List
, Format-Table
그리고 Format-Wide
행운과 함께. 원래 다음과 같은 것이 효과가있을 것이라고 생각했습니다.
Get-ChildItem -path hklm:\software\microsoft\windows\currentversion\uninstall `
| ForEach-Object -Process { "$_.GetValue("DisplayName") `n" }
그러나 그것은 나에게 오류를 주었다.
또는 출력 필드 구분 기호 (OFS)를 두 줄 줄 바꿈으로 설정 한 다음 파일로 보낼 때 문자열이 있는지 확인합니다.
$OFS = "`r`n`r`n"
"$( gci -path hklm:\software\microsoft\windows\currentversion\uninstall |
ForEach-Object -Process { write-output $_.GetValue('DisplayName') } )" |
out-file addrem.txt
를 사용 `하고 '. 내 키보드 (미국 영어 Qwerty 레이아웃)에서 1.
(댓글에서 여기로 이동-Koen Zomers에게 감사드립니다)
이것을 시도하십시오 :
PS> $nl = [Environment]::NewLine
PS> gci hklm:\software\microsoft\windows\currentversion\uninstall |
ForEach { $_.GetValue("DisplayName") } | Where {$_} | Sort |
Foreach {"$_$nl"} | Out-File addrem.txt -Enc ascii
내 addrem.txt 파일에 다음 텍스트가 생성됩니다.
Adobe AIR
Adobe Flash Player 10 ActiveX
...
참고 : 내 시스템에서 GetValue ( "DisplayName")는 일부 항목에 대해 null을 반환하므로이를 필터링합니다. BTW, 당신은 이것에 가깝습니다.
ForEach-Object -Process { "$_.GetValue("DisplayName") `n" }
문자열 내에서 변수의 속성, 즉 "표현식 평가"에 액세스해야하는 경우를 제외하고 다음과 같은 하위 표현식 구문을 사용해야합니다.
Foreach-Object -Process { "$($_.GetValue('DisplayName'))`r`n" }
기본적으로 큰 따옴표로 묶인 문자열 내에서 PowerShell은와 같은 변수를 확장 $_
하지만 다음 구문을 사용하여 하위 식에 식을 넣지 않는 한 식을 평가 하지 않습니다 .
$(`<Multiple statements can go in here`>).
궁극적으로 각 줄 사이에 EXTRA 빈 줄을 사용하여 수행하려는 작업은 약간 혼란 스럽습니다. :)
정말하고 싶은 것은 Get-ItemProperty를 사용하는 것입니다 . 값이 누락되면 오류가 발생하지만이를 억제 -ErrorAction 0
하거나 미리 알림으로 남겨 둘 수 있습니다. 레지스트리 공급자가 추가 속성을 반환하기 때문에 Get-Properties와 동일한 속성을 사용 하는 Select-Object 를 사용하는 것이 좋습니다.
그런 다음 줄 사이에 빈 줄이있는 각 속성을 원하면 Format-List를 사용합니다 (그렇지 않으면 Format-Table 을 사용하여 한 줄에 하나씩).
gci -path hklm:\software\microsoft\windows\currentversion\uninstall |
gp -Name DisplayName, InstallDate |
select DisplayName, InstallDate |
fl | out-file addrem.txt
나는 당신이 마지막 예에서 올바른 아이디어를 가지고 있다고 생각합니다. 이미 인용 된 문자열 안에 인용 부호를 넣으려고했기 때문에 오류가 발생했습니다. 이것은 그것을 고칠 것입니다 :
gci -path hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object -Process { write-output ($_.GetValue("DisplayName") + "`n") }
Edit: Keith's $() operator actually creates a better syntax (I always forget about this one). You can also escape quotes inside quotes as so:
gci -path hklm:\software\microsoft\windows\currentversion\uninstall | ForEach-Object -Process { write-output "$($_.GetValue(`"DisplayName`"))`n" }
The option that I tend to use, mostly because it's simple and I don't have to think, is using Write-Output as below. Write-Output will put an EOL marker in the string for you and you can simply output the finished string.
Write-Output $stringThatNeedsEOLMarker | Out-File -FilePath PathToFile -Append
Alternatively, you could also just build the entire string using Write-Output and then push the finished string into Out-File.
참고URL : https://stackoverflow.com/questions/1639291/how-do-i-add-a-newline-to-command-output-in-powershell
'Program Tip' 카테고리의 다른 글
C ++에서 typedef를 언제 사용해야합니까? (0) | 2020.11.16 |
---|---|
ERRORLEVEL을 0으로 재설정하는 가장 쉬운 방법은 무엇입니까? (0) | 2020.11.16 |
대화 상자 내에서 jQuery 대화 상자를 닫는 방법은 무엇입니까? (0) | 2020.11.16 |
문자열이 double로 구문 분석 가능한지 확인하는 방법은 무엇입니까? (0) | 2020.11.16 |
VC2010 C ++-소스 파일 구성 (0) | 2020.11.16 |