Displaying block of text - powershell

I've done a bit of Googling and can't seem to find an effective method of displaying an entire block of text to the console. I would rather not use the Write-Host command on every line if I need to display a block of code. I'm trying to make an interactive script that's somewhat aesthetic. Is there an example that someone could give me?

PowerShell supports multiline strings, either as here-strings:
Write-Host #"
Some text you
want to span
multiple lines.
"#
or regular strings:
Write-Host "Some text you
want to span
multiple lines."

In addition to Ansgar's examples, Write-Host accepts an array too.
'one','two','three' | Write-Host
So whether your multi-line string is a single string, or an array of lines, it will still work as expected with a single Write-Host call:
Get-Content mycode.txt | Write-Host
Get-Content mycode.txt -Raw | Write-Host

Related

Usage of Write-Output is very unreliable compared to Write-Host

I was pointed at a question that suggests using Write-Output over Write-Host if I want commands to operate sequentially (as Write-Host doesn't put the output on the pipeline while other commands do, which means that Write-Host output can happen before or after other commands that are on the pipeline leading to very messy output): command execution ordering inside a PowerShell scriptblock
Following this advice, I made a simple function using Write-Output to mimic Write-Host's colour syntax. For ordering, this works well, so that output from commands is now sequential, but the colour output is now awful with Write-Output so that if I use any BackgroundColor at all, the results are sprayed over the screen in very ugly ways. Write-Host was tight and reliable with colour output and didn't bleed into other parts of the console so using Write-Output with colour makes for some really ugly/clunky console output.
Do I need to reset $host.ui in some way before leaving the function, or can anyone suggest a way to modify this function so that the colours remain tight to the areas that they are required for and don't bleed to other console areas?
function Write-Color ($text, $ForegroundColor, $BackgroundColor) {
$defaultFore = $host.ui.RawUI.ForegroundColor
$defaultBack = $host.ui.RawUI.BackgroundColor
if ($ForegroundColor -ne $null) { $host.ui.RawUI.ForegroundColor = $ForegroundColor }
if ($BackgroundColor -ne $null) { $host.ui.RawUI.BackgroundColor = $BackgroundColor }
Write-Output $text
$host.ui.RawUI.ForegroundColor = $defaultFore
$host.ui.RawUI.BackgroundColor = $defaultBack
}
e.g.
Write-Color "The dog sat on the couch" -ForegroundColor Red -BackgroundColor White
Write-Host is the right tool for producing (possibly colored) for-display output - as opposed to outputting data via PowerShell's success output stream, via cmdlet calls and expressions, (optionally via explicit Write-Output calls, but that's rarely needed).
This answer explains that if you mix Write-Host and success-stream output, in PowerShell v5+ what prints to the console can appear out of order.
This is a side effect of implicitly applied tabular formatting situationally being asynchronous, in an effort to collect some data before printing output so as to determine suitable column width. It happens only for output types that (a) don't have predefined format data, and (b) have 4 or fewer properties (because types with more properties default to list formatting).
The problematic behavior is discussed in GitHub issue #4594; while there's still hope for a solution, there has been no activity in a long time.
There is no good solution to this problem as of PowerShell 7.0:
There are two - suboptimal - workarounds:
(a) Pipe individual commands that trigger the asynchronous behavior to ... | Out-Host.
E.g., in the following command the command with the Select-Object call must be sent to Out-Host so as to appear correctly between the two Write-Host calls on the screen:
Write-Host '------- 1'
Get-Item . | Select-Object FullName | Out-Host
Write-Host '------- 2'
Downside: Using Out-Host means you lose the ability to capture or redirect the command's output, because it is sent directly to the host (display). Additionally, it is cumbersome to (a) know what commands trigger the problem and (b) to remember to apply the workaround to each.
(b) Replace Write-Host calls with sending strings with embedded VT (Virtual Terminal) escape sequences (for coloring) to the success output stream.
Note: Requires Windows PowerShell v5.1 on Windows 10 or PowerShell [Core] v6+
Downside: The (colored) strings become part of the code's data output and are therefore included when you capture / redirect output.
# Windows PowerShell 5.1: [char] 0x1b produces an ESC char.
$green = [char] 0x1b + '[32m'; $reset = [char] 0x1b + '[m'
# Print "green" in green.
"It ain't easy being ${green}green${reset}."
# PowerShell 6+: `e can be used inside "..." for ESC.
$yellow = "`e[33m"; $reset = "`e[m"
# Print "yellow" in yellow.
"They call me mellow ${yellow}yellow${reset}."
The fact that these strings contain ESC chars. could actually be used to filter out for-display strings from the data stream (assuming your actual data doesn't contain ESC chars.), along the lines of ... | Where-Object { -not ($_ -is [string] -and $_ -match '\e') }
Embedding VT escape sequences allows you to selectively color parts of your strings.
Achieving the same effect with Write-Host would require multiple calls with -NoNewline.
Third-party cmdlet (module) Write-ColoredOutput emulates Write-Host's syntax and uses the [console] type's attributes to turn coloring on and off, while sending the string to the success output stream.
This works well for writing an entire string in a given color, but you cannot piece together differently colored parts on a single line, because each string individually written to the success output stream invariably prints on its own line.
If you wanted a convenience wrapper around embedding VT sequences directly in strings, you could adapt the Write-HostColored function from this answer, by replacing the Write-Host calls that happen behind the scenes with VT sequences.

Export variables into two columns

"$psIP
$psName
$psPower $psVar1
$psWattage $psVar2
$psRED $psVar3
$psHE $psVar4
$psL1 $psVar5
$psP1 $psVar6
$psStat1 $psVar7
$psL2 $psVar8
$psP2 $psVar9
$psStat2 $psVar10"| Out-File -FilePath P:\root\ResultFolder\Fingers1.csv
Sorry I am very new to powershell but I do have some background in coding.
I have set two sets of variables all in the same script. The goal is to have them in two columns so that way when the script executes it can be easily filtered. I need to know how to specify which VAR goes to which column. Please ask any question.
If you're looking to create a csv file with specific values in each row/column - see if this is what you're looking for:
#"
$psIP,$psName
$psPower,$psVar1
$psWattage,$psVar2
$psRED,$psVar3
$psHE,$psVar4
$psL1,$psVar5
$psP1,$psVar6
$psStat1,$psVar7
$psL2,$psVar8
$psP2,$psVar9
$psStat2,$psVar10
"# -split [System.Environment]::NewLine | Out-File -FilePath P:\root\ResultFolder\Fingers1.csv -Encoding ascii
The solution uses a Here String piped to split operator to break up the rows.
Also notice i'm using -Encoding ascii - this is so that Excel will see the file correctly when you open it.

-NoNewLine Not working as expected in Powershell

I am using powershell to concatenate a key but the Writehost is giving me a carriage delimiter and 3 lines instead of a single line when using -NoNewLine.
I have tried -NoNewLine along with various other code sets such as
write-host -replace "[`r`n](-join(
Bearer
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw
ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C
cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t
PeIoyImIORI59k0Ee_Ay4jlSpiPT_FEdlln_ZWfcEDCPQ
I also have been scouring Stack overflow and everyone says having the no new line should be the answer
$assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw"
$assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C"
$assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"
$apikey = ($assoc1,$assoc2,$assoc3)
write-host $apikey -NoNewLine
I would expect this to run and the result to be one line
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"
instead the result set gives me 3 lines of codes instead of 1. Is this a limitation on powershell?
enter image description here
You can just join them, this way...
$assoc1,$assoc2,$assoc3 -join ''
Though this will show as 3 lines on screen, because of screen formatting, it is not.
($assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw" ).Length
($assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C" ).Length
($assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t").Length
($apikey = $assoc1,$assoc2,$assoc3 -join '').Length
# Results
220
220
220
660
$apikey.Count
1
$apikey | clip
If you paste this into notepad, without 'Word Wrap' on, you'll see it's one line.
Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t
If you want your 3 strings to be joined without separators, i.e., to be directly concatenated, use the unary form of the -join operator:
$apikey = -join $assoc1, $assoc2, $assoc3
If you want to output that single string without a trailing newline:
To print to the console only, you can use Write-Host -NoNewline $apikey
Note that as an artifact of display in the console your single-line string may still display across multiple lines, depending on the window / buffer width of your console window.
To save to a file, use Set-Content -NoNewline or Out-File -NoNewline (PSv5+).

How to skip text from file txt when using curl command in powershell

I am using Curl command to check connection of host but in a list i have text like that. So i want to use Curl command to read this list and skip texts such as TEXT1 TEXT2 TEXT3 but still show them into screen.
Everyone, please help me how to skip it. Thank you for your helping!
TEXT1
10.0.254.161:9080
TEXT2
10.0.140.11:8866
TEXT3
10.0.110.96
There are several ways to do this.
First if you wish to show and pass through some items you can first write each item to verbose, host, information streams, and then at the end filter for the things you wish "to keep" or pass through to the next command.
Writing to verbose frequently makes a lot of sense as you can turn it off and own with your own preferences: $VerbosePreference defaults to: SilentlyContinue
Try this for a start but please understand this is not a very "PowerShell" way to do things, just an answer to your actual question:
For purposes of testing I loaded your sample data into the variable "$curl". You can just pipe the output of your actual "curl" command to the ForEach-Object at the start if you prefer:
$curl -split '\n' | # I have single string so split
ForEach-Object -begin {
$buffer=#() # initialize empty array
} -process {
write-host $_ -fore blue -back white # show all blue on white
$buffer += $_ # add each item to buffer
} -end {
$buffer # hold buffer until end
} | Where-Object { $_ -match '[\d.:]{7,}' } # filter for just IP:Port
You'll get an obvious (Blue on White) printing of everything, but at the end you'll get just the IP:Port patterns to the standard output, which you can capture to a variable, send to a file, or pipe to another command.
This match is looking for at least 7 Digits, DOTs, or COLONs, (4.4.4.4 would be about a minimal IP), but it isn't very sophisticated and would get fooled by phone numbers (without dashes or spaces):
$_ -match '[\d.:]{7,}' # any 7 digits, periods or colons will match
You can work a lot harder to match an "IP with optional Port number" if you wish, something like:
$_ -match '(\d{1,3}\.){3}(\d{1,3})(:(\d{1,5}))?' # picky match
...but it returns the same (35) items for your sample data.
1to3 digits and a dot, 3 times, then 1to3 digits, optionally followed by a COLON and 1 to 5 digits.
You could wrap it in ^ beginning of line and $ end of line if you wanted to be really stringent.
The above is really ugly for PowerShell but represent a 'minimalist' answer to the question.

Unnecessary space in output when using Write-Host

When I use Write-Host within a Foreach-Object, I get an unnecessary space in the output.
write-host "http://contoso.com/personal/"$_.ADUserName
Output:
http://contoso.com/personal/ john.doe
How can I remove the space before john? Trim does not work because there is no space in $_.ADUserName
This is happening because Write-Host is considering your constant string and your object to be two separate parameters -- you aren't actually joining the strings together the way you're calling it. Instead of calling it this way, actually concatenate the strings:
write-host "http://contoso.com/personal/$($_.ADUserName)"
or
write-host ("http://contoso.com/personal/" + $_.ADUserName)
or
write-host ("http://contoso.com/personal/{0}" -f $_.ADUserName)
Just do it without write-host:
"http://contoso.com/personal/{0}" -f $_.ADUserName