Skip to main content
St Louis

Back to all posts

How to Split A Variable With A Space In Powershell?

Published on
4 min read
How to Split A Variable With A Space In Powershell? image

Best Tools for PowerShell Script Development to Buy in October 2025

+
ONE MORE?

To split a variable with a space in PowerShell, you can use the Split method. For example, if you have a variable $myVariable containing a string with spaces, you can split it into an array using the following syntax: $myArray = $myVariable -split ' ' This will split the string contained in $myVariable at each space and store the resulting substrings in an array $myArray. You can then access each element of the array using indexing ($myArray[0], $myArray[1], etc.).

How to split a string into an array using space as a separator in PowerShell?

In PowerShell, you can split a string into an array using the -split operator. Here's an example of how you can split a string into an array using space as a separator:

# Original string $string = "This is a sample string"

Splitting the string into an array using space as separator

$array = $string -split ' '

Output the array elements

$array

In this example, the original string "This is a sample string" is split into an array using the -split operator with a space (' ') as the separator. The resulting array will contain the individual words as elements.

How to split a sentence into separate components using space in PowerShell?

You can split a sentence into separate components using the -split operator in PowerShell. Here's an example:

$sentence = "This is a sample sentence." $components = $sentence -split " " $components

In this code snippet, the sentence "This is a sample sentence." is split into separate components based on the space character. The resulting array $components will contain ["This", "is", "a", "sample", "sentence."].

You can also access individual components by indexing the array, for example $components[0] would give you "This", $components[1] would give you "is", and so on.

What is the easiest way to split a variable by space in PowerShell?

The easiest way to split a variable by space in PowerShell is by using the -split operator.

For example, if you have a variable $str that contains a space-separated string, you can split it into an array of substrings using the following code:

$str = "Hello World" $array = $str -split " "

This will split the string "Hello World" into an array $array with two elements, "Hello" and "World".

How can I divide a variable using a space in PowerShell?

To split a variable using a space in PowerShell, you can use the -split operator. Here is an example:

$variable = "Hello World" $splitVariable = $variable -split " "

In this example, the variable $variable is divided into an array of strings based on the space character. The resulting array, $splitVariable, will contain two elements: "Hello" and "World".

You can access each element in the array using the index like this:

$splitVariable[0] # Output: Hello $splitVariable[1] # Output: World

Alternatively, you can use the Split() method with the StringSplitOptions.RemoveEmptyEntries option to remove any empty entries:

$variable = "Hello World" $splitVariable = $variable.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)

In this example, the split variable will only contain "Hello" and "World", even with multiple spaces between them.

How to parse a string using a space as a delimiter in PowerShell?

To parse a string using a space as a delimiter in PowerShell, you can use the -split operator. Here's an example:

$string = "Hello World How Are You" $words = $string -split " " $words

In this example, the -split operator splits the $string variable into an array of words using a space character as the delimiter. The resulting array $words will contain each word in the string as a separate element.

You can then access each word in the $words array using indexing. For example, to access the first word, you can use $words[0], the second word with $words[1], and so on.