EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Set Variable in PowerShell
Secondary Sidebar
PowerShell Tutorial
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Set Variable in PowerShell

By Priya PedamkarPriya Pedamkar

Set Variable in PowerShell

Introduction to Set-Variable in PowerShell

The following article provides an outline of the set-variable in Powershell. A variable is a block of memory that can be used to store values. PowerShell variables are denoted using the “$” symbol. A variable name can be anything from numbers, alphabets or even underscores. PowerShell variables are not case sensitive. PowerShell variables are not just text-based, instead, they are objects like that of Microsoft.Net objects. There are different types of variables like User-Created variables, automatic variable and preference variables.  Set-variable is the cmdlet to set the value of a variable. In case if the variable doesn’t exist, the set-variable cmdlet creates one and then assigns the value to it.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Syntax for Set Variable in PowerShell

The following is the syntax of the set-variable cmdlet.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Input:

Get-Help Set-Variable

Output:

set variable in powershell

Example:

Set-Variable -Name "test" -Value "test variable"

The above cmdlet sets the value of a variable called test to “test variable”. If the variable doesn’t exist, it will be created.

Parameters of Set Variable in PowerShell

The following parameters are given below:

1. Confirm: This is used to get confirmation before running the cmdlet. This is an optional parameter. Its type is a switch. Its default value is none. It doesn’t accept pipeline input and wildcard characters.

2. Exclude: This specifies the list of items that should be excluded while executing the cmdlet. It can be a path or a pattern. Its type is a string. Its type is a switch. Its default value is none. It doesn’t accept pipeline input whereas it can accept wildcard characters. It is also an optional parameter.

3. Force: This allows the creation of a variable as same as an existing read-only variable. It can also be used to change the value of a read-only variable. This is an optional parameter. Its type is the switch. Its default value is false. It doesn’t accept pipeline input and wildcard characters.

4. Include: This specifies the list of items that should be included while executing the cmdlet. It can be a path or a pattern. Its type is a string. Its type is the switch. Its default value is none. It doesn’t accept pipeline input whereas it can accept wildcard characters. It is also an optional parameter.

5. Name: This specifies the variable name and it’s a mandatory parameter. Its type is a string. Its default value is none. It accepts pipeline input whereas wild card characters are not permitted.

6. Option: This is used to specify the variable’s options property. It is default value is none. Its type is the scope of items. It doesn’t accept pipeline input and wildcard characters. This is an optional parameter.

Its applicable values are as follows

  • Note: This is the default value; no options are set.
  • Readonly: The variable’s value cannot be changed except using the force parameter. The variable can also not be deleted.
  • Constant: The variable’s value cannot be changed or deleted. A variable can be made constant only during its creation.
  • Private: The variable is valid only in the current scope.
  • AllScope: The variable can be copied to any scope that may be created.

7. Scope: This parameter specifies the scope of the variable. The available scopes are Global, local, script and Private. Local is the default scope of a variable. Its type is a string. It doesn’t accept pipeline input and wildcard characters. This is an optional parameter.

8. Value: This is used to specify the variable’s value. Its type is the object and default value is none. It can accept pipeline input but doesn’t accept wild card characters.

9. Visibility: This denotes whether the variable created is available for outside the session in which it is created. Its value is either public or private. By default, it is public, which means it is available even outside the session. Private denotes the variable is not available outside the session. It doesn’t accept pipeline input and wildcard characters.

10. WhatIf: This shows the actual output if the cmdlet is run. It’s like a demo of the cmdlet. Type is a switch. The default value is false. It doesn’t accept pipeline input and wildcard characters.

Examples of Set-Variable in PowerShell

Below are the examples for the set-variable in PowerShell

Example #1

Set and Get a Variable

Input:

Set-Variable -Name "test" -Value "test"
Get-Variable -Name "test"
Set-Variable -Name "test1" -Value "testone"
Get-Variable -Name "test1"
Set-Variable -Name "test2" -Value "testtwo"
Get-Variable -Name "test2"
Write-Host "Get all the user-defined variables in the current session"
Get-Variable test* -ValueOnly

Output:

set variable in powershell1

Example #2

Difference between Private and Public Variable

Input:

Write-Host "Example of the public variable"
Set-Variable -Name "PubVar" -Visibility Public -Value "This is a Public Variable"
$pubvar
Write-Host "Example of the private variable"
Set-Variable -Name "PriVar" -Visibility private -Value "This is a private Variable"
$PriVar

Output:

set variable in powershell2

Working with Environment Variables

Below are the two examples for working with environment variables

1. Creating a local scoped Environment Variable

Input:

Write-Host "Creation of local environment variable"
$env:locevar = 'Local Environment variable'
Write-Host "Displaying the value of the variable"
Get-ChildItem Env:locevar

Output:

Creating local environment

In the above example, the created variable will be available only for the current PowerShell session. Once the session is closed, the variable will vanish.

2. Creating an environment variable for the machine

Input:

Write-Host "creation of environment variable applies to a system"
[System.Environment]::SetEnvironmentVariable('envvar','environment variable',[System.EnvironmentVariableTarget]::Machine)
Set-Location Env:
Get-ChildItem

Output:

Creating environment for machine

Conclusion – Set-Variable in PowerShell

Thus, the article explained in detail about the set-variable cmdlet along with its different scopes. It also explained about environment variables, how to create and access them. To learn in-depth about the cmdlet it is advisable to create sample programs and have fun working around them.

Recommended Articles

This is a guide to Set Variable in PowerShell. Here we discuss the parameters of Set Variable in PowerShell along with working with Environment Variables and respective examples. You can also go through our other related articles to learn more–

  1. PowerShell Date
  2. Comparison Operators in PowerShell
  3. Deep Learning Technique
  4. Switch Case in PowerShell
  5. Guide to PowerShell Set-Location
  6. Complete Guide to PowerShell Get-ChildItem
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more