Basics of Input and Output in Windows PowerShell

Windows PowerShell is a command-line and scripting language designed for system administration. It helps IT professionals, to control and automate the administration of the Windows Operating System and Windows Server Environment. There are cmdlet for console input and output using PowerShell.

Example 1: Showing the Output to console

Function to print a string.
Input : 'Geeks for Geeks' 
Output : Hello Geeks for Geeks 

Input : 'PowerShell Beginner'
Output : Hello PowerShell Beginner
filter_none

edit
close

play_arrow

link
brightness_4
code

# show function definition
function show {
      
    param ( $str2 )           
    #parameter string
  
    Write-Host "Hello $str2"  
    # output on console
  
}
  
$str = "Geeks for Geeks"      
# string declaration
  
show ( $str )                 
#function call with string

chevron_right


In Windows Powershell, the input and output are given through the Host. It uses ‘Write-Host’ to print and ‘Read-Host’ to get input from console.



Example 2: Taking the User input through console

Function to get user input.
Input : 'Geeks for Geeks' 
Output : Hello Geeks for Geeks 

Input : 'PowerShell Beginner'
Output : Hello PowerShell Beginner
filter_none

edit
close

play_arrow

link
brightness_4
code

# show function definition
function show {
      
    param ( $str2 )                            
    #parameter string
  
    Write-Host "Hello $str2"                   
    #output on console 
  
}
  
#user input with prompt
$str = Read-Host -Prompt 'Enter a String'
       
#function call with string
show ( $str )                                 

chevron_right


Note: If your script show error “script is Disabled” while running program then try to change the execution policy with administrative privilege by using the command ‘set-executionpolicy remotesigned



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.