본문 바로가기
ICT

[PowerShell]3. Inputs and Outputs

by NeoSailer 2023. 3. 23.

PowerShell에서 사용자에게 입력 값을 받아야 하는 경우 또는 결과물 및 데이터를 확인해야 하는 경우가 있다.

이 때 사용해야 하는 PowerShell의 입/출력 명령어에 대하여 알아보자.

 

[입력 처리 명령어]

Read-Host

사용자 입력을 받는 명령어는 "Read-Host"이다.

Read-Host (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

 

Read-Host (Microsoft.PowerShell.Utility) - PowerShell

The Read-Host cmdlet reads a line of input from the console (stdin). You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords. Note Read-Host ha

learn.microsoft.com

"Read-Host"를 사용하여 사용자 입력 값을 받아 그대로 출력해 보자.

* "Read-Host" 명령어의 인자는 사용자 입력 값 대기 중 표시되는 내용이다.

PS C:\Users\jaehui.yoon> $temp = Read-Host "Tell me how you feel today"
Tell me how you feel today: Fantastic!

 

[출력 처리 명령어]

Write-Host

문자나 변수 값을 임의적으로 화면에 출력하기 위해 사용하는 명령어는 "Write-Host"이다.

Write-Host (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

 

Write-Host (Microsoft.PowerShell.Utility) - PowerShell

The Write-Host cmdlet's primary purpose is to produce for-(host)-display-only output, such as printing colored text like when prompting the user for input in conjunction with Read-Host. Write-Host uses the ToString() method to write the output. By contrast

learn.microsoft.com

PowerShell 개발자 Jeffrey Snover는 "Write-Host"를 사용을 자제하는 것이 바람직하다고 주장한다.

때문에 변수 값의 확인이나 명령어 결과 출력 외에는 사용을 자제하는 것이 좋다. 

위에 "Read-Host"에서 입력 받았던 사용자 입력 값(Fantastic!)을 "Write-Host"를 사용하여 출력해 보자.

 

PS C:\Users\jaehui.yoon> Write-Host $temp
Fantastic!

 

Write-Output

명령어 처리 결과를 나타내 주는 명령어는 "Write-Output"이다.

Write-Output (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

 

Write-Output (Microsoft.PowerShell.Utility) - PowerShell

Writes the specified objects to the pipeline. If Write-Output is the last command in the pipeline, the objects are displayed in the console. Write-Output sends objects to the primary pipeline, also known as the "output stream" or the "success pipeline." To

learn.microsoft.com

 


"Write-Host"와 "Write-Output"의 차이

  "Write-Host"는 출력 값을 사용자가 직접 입력해야 하는 반면에 "Write-Output"은 출력 값이 있는 명령어를 사용 시 파이프라인을 통해 자동으로 적용된다. 예를 들어 "Get-Service" 명령어를 사용하면 서비스 목록이 자동으로 출력 되는데 비명시적으로 "Get-Service|Write-Output"의 명령어가 적용된 것이다. 다시 말해 "Get-Service"과 "Get-Service|Write-Output"은 같은 결과 값을 보여준다.


 

Get-Process

PS C:\Users\jaehui.yoon> Get-Process

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                  
-------  ------    -----      -----     ------     --  -- -----------                                                                                  
    228      17    17096      12268             18576   0 agentid-service

Get-Process | Write-Output

PS C:\Users\jaehui.yoon> Get-Process | Write-Output

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                  
-------  ------    -----      -----     ------     --  -- -----------                                                                                  
    228      17    17096      12268             18576   0 agentid-service
반응형

댓글