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 November 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
4 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$32.98 $44.99
Save 27%
Learn Windows PowerShell in a Month of Lunches
5 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
6 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
7 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$40.91 $44.99
Save 9%
Learn Windows PowerShell in a Month of Lunches
8 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$49.28
Learn PowerShell Toolmaking in a Month of Lunches
9 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
10 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$26.99 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
+
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.