Today, I will explain a very useful cmdlet: the PowerShell new-variable cmdlet. You will learn about the syntax and usage with various real examples.
The New-Variable cmdlet in PowerShell is used to create new variables. For a basic implementation, you can create a simple variable by using the command New-Variable -Name "Greeting" -Value "Hello, PowerShell!", which sets the variable Greeting with the value “Hello, PowerShell!”.
What is the PowerShell New-Variable cmdlet?
The New-Variable cmdlet creates a new variable in PowerShell. This cmdlet allows you to set variables with specific scopes, descriptions, and even visibility settings. Whether you’re managing a script’s internal variables or setting up environment-wide variables, New-Variable is your go-to tool.
Syntax of New-Variable
Here’s the basic syntax for New-Variable:
New-Variable [-Name] <String> [[-Value] <Object>] [-Description <String>] [-Option <ScopedItemOptions>] [-Scope <String>] [-Visibility <SessionStateEntryVisibility>] [-Force] [<CommonParameters>]Parameters:
- -Name: Specifies the name of the variable.
- -Value: Assigns a value to the variable.
- -Description: Provides a description for the variable.
- -Option: Sets options like ReadOnly, Constant, etc.
- -Scope: Defines the scope of the variable (e.g., Global, Local).
- -Visibility: Sets the visibility (e.g., Public, Private).
- -Force: Forces the creation of the variable even if it exists.
Read Trim Variable Before Character in PowerShell
PowerShell New-Variable
Now, let me show you some examples of how to use the new-variable PowerShell cmdlet.
1. Create a Simple PowerShell Variable
Let’s start with an example. Suppose I want to create a variable named Greeting with the value “Hello, PowerShell!”:
New-Variable -Name "Greeting" -Value "Hello, PowerShell!"You can see this in the screenshot below:

Check out PowerShell If Variable Contains
2. Add Descriptions and Options
You might want to add descriptions to your variables in more complex scripts for better readability and maintenance. For instance, if I’m working on a script that handles user data, I could do:
New-Variable -Name "UserName" -Value "JohnDoe" -Description "Stores the user's name" -Option ReadOnlyIn this example, the UserName variable is set to “JohnDoe” and is marked as ReadOnly, meaning it cannot be changed once set.
3. Using Scopes
Scope determines where the variable is accessible. For example, if I want a variable to be available globally across all scripts and sessions, I can set its scope to Global:
New-Variable -Name "GlobalVar" -Value "I am global" -Scope GlobalRead PowerShell Private Variables
4. Visibility Settings
Visibility settings control whether a variable is accessible from outside the module or script where it is defined. For example, setting a variable to Private ensures it’s only accessible within the script or module:
New-Variable -Name "PrivateVar" -Value "I am private" -Visibility PrivateRead PowerShell If Variable Equals
PowerShell New-Variable Examples
Now, let me show you some examples of PowerShell New-variable.
Example 1: Manage Environment Variables
Suppose you’re setting up a script to configure environment variables for a development environment. You can use New-Variable to set these up:
New-Variable -Name "DevPath" -Value "C:\Dev\Environment" -Scope GlobalExample 2: Handle User Data
You might want to store user information securely in a script that processes user data. Here’s how you can create variables to store user details. Here is the PowerShell script.
New-Variable -Name "UserEmail" -Value "john.doe@example.com" -Description "Stores the user's email address"
New-Variable -Name "UserAge" -Value 30 -Description "Stores the user's age"Check out Clear Variables in PowerShell
Example 3: Set Up Configuration Variables
You might want to create variables that store configuration settings when working with configurations. Here is the complete PowerShell script.
New-Variable -Name "ConfigPath" -Value "C:\Config\Settings.json" -Description "Path to the configuration file" -Option ConstantIn this example, ConfigPath is set as a constant, meaning it cannot be changed or deleted once set.
Conclusion
I hope this tutorial helps you to learn how to use New-Variable in PowerShell. I have explained in detail with some examples.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.