EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell null

PowerShell null

Updated March 6, 2023

PowerShell null

Introduction to PowerShell null

The following article provides an outline for PowerShell null. PowerShell $Null is an automatic variable that holds nothing but unidentified or absent value and it is also considered as an Object. PowerShell treats $Null object with the value null and some commands require some output to generate and they generate value null if there is any error and can also be useful in the troubleshooting purpose in the scripts to check if the command generates any value.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Although there is no specific syntax for null, we can use the automatic and predefined variable $NULL for this.

How does $null Variable Works in PowerShell?

When you type $Null, it produces nothing as shown below.

Code:

$null

When you declare a variable and assign nothing, the value by default it holds is the $NULL.

Code:

$a
$a -eq $null

Output:

PowerShell null 1

You can also assign $null value explicitly to a variable as shown below.

Code:

$val = $null
Write-Output "The value is : $val"

Output:

PowerShell null 2

Many times when writing a script we need to spot if the value is Null but it doesn’t visible in the console. To spot if the variable is going to hold a null value, we need to use the special characters or something that we can identify that the variable holds a null value. We will use it here {}. You can use anything for it like other brackets.

Code:

$val = $null
Write-Output "The value is : {$val}"

Output:

PowerShell null 3

Examples of PowerShell null

Given below are the examples of PowerShell null:

Example #1

Difference between “” and $NULL.

Although both (“” and $NULL) represent the empty value there is a quite difference between them.

The first thing is both are not equal.

Code:

"" -eq $null

Output:

PowerShell null 4

When you declare a variable without any value, its output is $NULL.

Code:

$a
$a -eq $null

Output:

declare a variable without any value

But when you compare it with the “” value, the output will be false.

Code:

$a -eq ""

Output:

PowerShell null 6

“” represents the empty space. This means when you assign that value to the variable, its output will be one blank line while $null output is nothing.

Code:

$a = ""
$a

Output:

PowerShell null 7

Now $a is not $null.

Code:

$a -eq $null

Output:

Now $a is not $null

Another way to check this is with the .NET method that the $null value is not similar to “” using String class.

Code:

$null -eq [String]::Empty

Output:

PowerShell null 9

Example #2

Null is an Undefined variable.

Any variable whose values are not initialized is considered as the undefined variable.

In this example, $service is not defined earlier so its value is not initialized and it is considered Null.

Code:

$null -eq $service

Output:

Undefined variable

Example #3

Null with Function.

When the function returns nothing then its output is Null.

Code:

Function ReturnsNull{}
$val = ReturnsNull
$val -eq $null

Output:

PowerShell null 11

The above output is true because the function doesn’t return anything.

The above example is similar to the below examples because they don’t return anything.

Code:

Function ReturnsNull{ return }
$val = ReturnsNull
$val -eq $null

Output:

don’t return anything

Another example of function.

Code:

Function ReturnsNull{
$svc = Get-Service NonExist -EA Ignore
if($svc){ return $svc }
}
$val = ReturnsNull
$val -eq $null

Output:

PowerShell null 13

Here, service doesn’t exist so the function returns Null.

Example #4

Null values for the PowerShell function scope.

When variables are called outside their scope, their values are always null because they are uninitialized values for the outside scope.

Code:

Function TestScope($a){
Write-Output "Value of a : $a"
$b = 20
}
$a = 10
TestScope $a
Write-Output "Value of b : [$b]"

Output:

 function Scope

In the above example, the value of $a variable is known to the function TestScope because it was declared outside the function but $b value is declared inside the function so that is unknown to the outside of the function and hence the value of $b variable shows NULL. To get the $b value outside of the function, you need to use the return command and need to capture the value from the function. This also works when you define $b as a global variable outside of the function and change its value in the scope.

Example #5

Null with numeric equation.

When we use the $null value with the numeric equation, it behaves similarly to the Value 0.

Code: 

$null + 1
$null + 5 + 10
4 + $null

Output:

Numeric equation

For multiplication, it depends on the position of the $Null value.

Code:

$null * 4
4 * $null

Output:

PowerShell null 17

It works the same for the divide operation and $null is considered as 0.

Code:

$null / 10
10 / $null

Output:

PowerShell null 18

Example #6

Null value with array.

When we call the index out of the bound of an array, it gives the null output.

Code:

$Colors = "Red","Yellow","Pink"

In this example, we have an array with 3 values, and if we call Colors array with the index 4, it will give a null value because that index doesn’t exist.

Code:

$Colors[4] $Colors[4] -eq $null

Output:

PowerShell null 19

In the array case, $null is not treated as 0. It generates an exception when it is used for an index.

Code:

$Colors[$null]

Output:

PowerShell null 20

Conclusion

Every variable assignment and function return values start with null until they are defined. They are negligible for any script because it doesn’t show any output for it but can be a big threat to a script if you are using the value of the one output to another one and the output is null. For large scripts, troubleshooting can be cumbersome if the null is not treated properly.

Recommended Articles

This is a guide to PowerShell null. Here we discuss the introduction, how does $null variable works in PowerShell? and examples respectively. You may also have a look at the following articles to learn more –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1
 19+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

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

Let’s Get Started

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

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

Forgot Password?

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