How To Span A PowerShell Command Over Multiple Lines
Think you can do better? Show us!
In this video, Bryce will cover how to write a function with a significant number of parameters, using backticks at the end of each line to make it human-readable in a script. Next, we’ll use a pipeline after a cmdlet to show how you can use the pipe over multiple lines as well.
this snip, I'm going to show you how to have a multiline commandlet or basically take a commandlet and span it over multiple lines makes it a lot easier to read. Whenever you're going through your scripts being able to have maybe each parameter on its own line. So here we have a commandlet write host pretty familiar with it and we are going to just say this is my multiline command line input and it's hard to see. And I've tried to make my font's largest possible but there is a backtick. Character at the end of that in that backtick what it does is it escapes the. Carriage return that follows right after so it escapes that carriage return so it looks like that foreground color is right next to the end of that quote that I have being sent into write host so same on the next line. I have another backtick that escapes that carriage return so that way. I can have my background color in there as well, and you'll find once I select all of this and. Run the selection that we are able to get a write host output with the foreground color sign in in the background color of dark magenta. One thing you will notice if you are using the ISE, though, is if you just try and like normal put it on put your cursor on the end of the command and Press. F8 it will only send what's on that line, so you do have to select the whole command that you're looking to send in the ISE. There's also a second way that we can span a command across multiple lines and that's actually by using the pipe. So here's one where we're going to get our process Chrome. I'm just going to stop my Chrome browser, I might regret that later. But here we will use get process. Find our Chrome process and then you see that pipe at the end of that line there? What we can do is actually use our stock process on the next line because the pipe will send it all the way through to the next commandlet so here we go will select the whole thing again Press F8 and if I were to look at my taskbar looks like Chrome is no longer running so I will go reset my tabs thanks for watching.