Here we will discuss about reading multi line inputs from command line using PowerShell. In some time we often get requirement to enter multiple lines or paragraphs of text as input to a PowerShell script, so we are going to discuss in this article.
Here logic is very simple, the Get-MultiLineInput function will repeatedly call Read-Host till the value entered by the user with โexitโ. If user enter with โexitโ then immediately it will stopping the loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
################################################################# #Project : How to Read Multiline User Input in Powershell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ################################################################## function Get-MultiLineUserInputs { [CmdletBinding()] param( ) $userinputstring = @() $userinput = $null while($userinput -ne "exit") { $userinput = Read-Host if($userinput -eq "exit") { Continue } else { $userinputstring += $userinput } } return $userinputstring } Write-Host "`nEnter your inputs (type 'exit' to finish)`n" -ForegroundColor Green $multilines = Get-MultiLineUserInputs Write-Host "`nOUTPUT`n" -ForegroundColor Green -join $multilines |
Leave A Comment