How to PowerShell Multiline String

Migel Hewage Nimesha Feb 02, 2024
How to PowerShell Multiline String

Different tools can be used for scripting and automation purposes. However, a tool as simple and powerful as PowerShell is not there for simple scripting, automation, and configuration management purposes.

PowerShell by Microsoft is a tool that comes in default with Windows operating systems, enabling Windows users with the automation functions based on command-line interfaces and commands.

Furthermore, there are versions of PowerShell for macOS versions as well. You have to find the correct version for your version of macOS.

Also, you can use PowerShell with Linux systems.

We expect to discuss PowerShell commands in the PowerShell console through this article.

PowerShell console is a command-line interface that can use to input commands to achieve your tasks, use as a scripting interface to enter the correct scripting commands, and do automation through that.

However, you need to know some basic commands of PowerShell to direct or navigate through the command-line interface of PowerShell.

Sometimes with specific commands, you would feel stuck in the console as the application may be waiting for a particular action to happen or a particular command to be given to exit the wait.

Here we will focus on one specific incident of PowerShell users being stuck in one place once a command is given. That is when it is required by the user to enter multiple lines in the console rather than a single line command.

Use of @' and '@ to Enter Multiple Line Strings in the Console of PowerShell

Some users have issues with including a multiple-line comment in the console of PowerShell.

The use of @' and '@ is the most accepted solution for this problem. Then the console would let you enter the following line without an issue after the '@.

The following code fragment shows the way can do it.

PS C:\Users\Agni> $Variable=@'
>> test line
>> test line
>> test line
>> '@
PS C:\Users\Agni>

We expect this to happen in the following code is the last line of the above code.

The console allows us to enter out the following command, ending the multi-line command, rather the giving >> to enter the following line of the same command.

However, entering the last '@ at the same final line will not work correctly. It will give >> to enter the following line of the multiple-line string.

PS C:\Users\Agni> $Variable=@'
>> test line
>> test line
>> test line '@

As you can see, the following typical command cannot be entered above.

Therefore, adding multiple lines uses @' and '@.

Yet you have to enter the first delimiter at the end of the first line and the last one as the previous line separately to enter and exit the multi-line string command properly.

Like this, there are some basic things you have to learn in PowerShell if you are to use PowerShell as your day-to-day scripting and automation in your work. We will discuss them in other articles.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell String