Skip to main content
TopMiniSite

Back to all posts

How to Store Powershell Output to A Variable?

Published on
3 min read
How to Store Powershell Output to A Variable? image

Best PowerShell Books to Buy in January 2026

1 Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

BUY & SAVE
$35.52 $39.99
Save 11%
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS
2 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

  • STREAMLINE IT TASKS WITH PRACTICAL POWERSHELL AUTOMATION SKILLS.
  • ACCESSIBLE GUIDE FOR SYSADMINS TO BOOST EFFICIENCY EFFORTLESSLY.
  • HANDS-ON WORKBOOK FORMAT FOR REAL-WORLD APPLICATION AND MASTERY.
BUY & SAVE
$24.49 $39.99
Save 39%
PowerShell for Sysadmins: Workflow Automation Made Easy
3 Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

BUY & SAVE
$24.99
Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises
4 PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

BUY & SAVE
$49.49 $89.99
Save 45%
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell
5 Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

BUY & SAVE
$32.49 $49.99
Save 35%
Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell
6 PowerShell Pocket Reference: Portable Help for PowerShell Scripters

PowerShell Pocket Reference: Portable Help for PowerShell Scripters

BUY & SAVE
$18.99 $29.99
Save 37%
PowerShell Pocket Reference: Portable Help for PowerShell Scripters
7 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.81 $59.99
Save 20%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
8 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$49.95
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
9 Windows PowerShell in Action

Windows PowerShell in Action

  • BRAND NEW, SEALED PACKAGING ENSURES QUALITY AND RELIABILITY.
  • INCLUDES ALL ESSENTIAL ACCESSORIES FOR IMMEDIATE USE.
  • FAST SHIPPING GUARANTEES QUICK DELIVERY TO YOUR DOORSTEP.
BUY & SAVE
$56.99 $59.99
Save 5%
Windows PowerShell in Action
+
ONE MORE?

To store PowerShell output to a variable, you can use the assignment operator "=" followed by the command you want to store the output of. For example, you can store the output of a command like Get-Process by assigning it to a variable like $processes = Get-Process. This will store the result of the command in the variable $processes, which you can then use in your script or program.

How to store PowerShell output to a variable in a script?

To store PowerShell output to a variable in a script, you can use the following syntax:

$variable = PowerShell command

Here's an example:

$output = Get-Process

In this example, the output of the Get-Process command is stored in the $output variable. You can then use the $output variable to access and manipulate the output as needed in your script.

How to redirect PowerShell output to multiple variables?

You can redirect PowerShell output to multiple variables by using the following syntax:

$var1, $var2 = command

For example, if you want to capture the output of the Get-Process command into two variables, you can use the following code:

$processes, $processCount = Get-Process

This will store the list of processes in the $processes variable and the count of processes in the $processCount variable.

How to assign PowerShell output to multiple variables?

To assign PowerShell output to multiple variables, you can use the following syntax:

$variable1, $variable2 = Get-Date

In this example, the output of the Get-Date cmdlet is assigned to two variables $variable1 and $variable2. Both variables will contain the current date and time in this case.

You can also assign output to multiple variables by using an array:

$variables = Get-Date $variable1 = $variables[0] $variable2 = $variables[1]

This will store the output of Get-Date in an array variable $variables, and then assign the first and second elements of the array to $variable1 and $variable2 respectively.

Additionally, you can use the Select-Object cmdlet to select specific properties from the output and assign them to variables:

$variable1, $variable2 = Get-Process | Select-Object -ExpandProperty ID, Name

In this example, the Get-Process cmdlet retrieves information about running processes, and the Select-Object -ExpandProperty cmdlet selects the ID and Name properties and assigns them to $variable1 and $variable2.

How to store a string in a variable in PowerShell?

To store a string in a variable in PowerShell, you can simply assign the string to a variable name using the following syntax:

$variableName = "YourStringHere"

For example, if you want to store the string "Hello, World!" in a variable named $myString, you would do the following:

$myString = "Hello, World!"

Now, the string "Hello, World!" is stored in the variable $myString and you can access it by referencing the variable name in your PowerShell script or command.