PowerShell Test-Path Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Get-Location And Set-Location cmdlets in PowerShell.
https://cloudaffaire.com/powershell-get-location-and-set-location-cmdlet/
In this blog post, we will discuss Test-Path cmdlets in PowerShell. You can use Test-Path cmdlet to check if an element in a specific path exists. Using Test-Path cmdlet you can test if an item like a registry key, file, directory, or variable exists or not. Test-Path cmdlet returns $True if the item exists and $False if the item is missing. You can also check if an item is created after a certain time using ‘NerwerThan’ and ‘OlderThan’ arguments.
Test-Path Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 |
## Test-Path ## [-Path] ## [-Filter ## [-Include ## [-Exclude ## [-PathType ## [-IsValid] ## [-Credential ## [-UseTransaction] ## [-OlderThan ## [-NewerThan ## [ |
Test-Path Cmdlet Argument List:
- –Credential: To impersonate another user or elevate your credentials.
- –Exclude: Specifies items that this cmdlet omits. The value of this parameter qualifies the Path parameter.
- –Filter: Specifies a filter in the format or language of the provider. The value of this parameter qualifies the Path parameter.
- –Include: Specifies paths that this cmdlet tests. The value of this parameter qualifies the Path parameter.
- –IsValid: Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist.
- –LiteralPath: Specifies a path to be tested. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed.
- –NewerThan: Specify a time as a DateTime object.
- –OlderThan: Specify a time as a DateTime object.
- –Path: Specifies a path to be tested. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.
- –PathType: Specifies the type of the final element in the path. This cmdlet returns $True if the element is of the specified type and $False if it is not. The acceptable values for this parameter are:
- Container: An element that contains other elements, such as a directory or registry key.
- Leaf: An element that does not contain other elements, such as a file.
- Any: Either a container or a leaf.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
PowerShell Test-Path Cmdlet:
Check If A Directory Exist In PowerShell:
1 2 3 4 5 6 7 8 |
##################################### ## PowerShell | Cmdlet | Test-Path ## ##################################### ## PowerShell Latest Version (5) ## check if a directory exists in PowerShell Test-Path -Path "C:\MyDir\" ## returns False |
Create A Directory If Not Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## create a directory if not already exist in PowerShell if(!(Test-Path -Path "C:\MyDir\")) { New-Item -ItemType Directory -Path C:\ -Name MyDir } Test-Path -Path "C:\MyDir\" ## returns True |
Check If A File Exist In PowerShell:
1 2 3 |
## check if a file exists in PowerShell Test-Path -Path "C:\MyDir\Myfile.txt" ## returns False |
Create A File If Not Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## create a file if not already exist in PowerShell if(!(Test-Path -Path "C:\MyDir\MyFile.txt")) { New-Item -ItemType Directory -Path C:\MyDir -Name MyFile.txt } Test-Path -Path "C:\MyDir\MyFile.txt" ## returns True |
Check If A Variable Is Declared In PowerShell:
1 2 3 |
## check if a variable is already declared in PowerShell Test-Path -Path Variable:MyVar ## returns False |
Declare A Variable If Not Already Declared In PowerShell:
1 2 3 4 5 6 7 |
## create a variable if not already exist in PowerShell If(!(Test-Path -Path Variable:MyVar)) { $MyVar = "CloudAffaire" } Test-Path -Path Variable:MyVar ## returns True |
Check If A Registry Entry Exist In PowerShell:
1 2 3 |
## check if a registry key exists in PowerShell Test-Path -Path HKCU:\Software\MyRegKey ## returns False |
Create A Registry Key If Not Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## create a registry key if not already exist in PowerShell if(!(Test-Path -Path HKCU:\Software\MyRegKey)) { New-Item -Path HKCU:\Software -Name MyRegKey -Value "CloudAffaire" } Test-Path -Path HKCU:\Software\MyRegKey ## returns True |
Check If A Directory Or File Is Creates Before Or After Certain Time In PowerShell:
1 2 3 4 5 |
## check if a file or directory created after a certain time $time = [DateTime]::Today.AddDays(-1) ## get DateTime 1 day back from today Test-Path -Path C:\MyDir -NewerThan $time ## returns True Test-Path -Path C:\MyDir -OlderThan $time ## returns False |
Remove A File If Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## remove a file if exist in PowerShell if((Test-Path -Path C:\MyDir\MyFile.txt)) { Remove-Item -Path C:\MyDir\MyFile.txt } Test-Path -Path C:\MyDir\MyFile.txt ## returns False |
Remove A Directory If Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## remove a directory if exist in PowerShell if((Test-Path -Path C:\MyDir)) { Remove-Item -Path C:\MyDir -Force -Recurse } Test-Path -Path C:\MyDir ## returns False |
Remove A Registry Key If Already Exist In PowerShell:
1 2 3 4 5 6 7 |
## remove a registry key if exist in PowerShell if((Test-Path -Path HKCU:\Software\MyRegKey)) { Remove-Item -Path HKCU:\Software\MyRegKey } Test-Path -Path HKCU:\Software\MyRegKey ## returns False |
Remove A Variable If Already Declared In PowerShell:
1 2 3 4 5 6 7 |
## remove a variable if already exist in PowerShell if((Test-Path -Path Variable:MyVar)) { Remove-Item -Path Variable:MyVar } Test-Path -Path Variable:MyVar ## returns False |
Hope you have enjoyed this article. In the next blog post, we will discuss Split-Path cmdlet in PowerShell.
To Set more details on PowerShell, kindly follow below official documentation