You are currently viewing PowerShell Test-Path Cmdlet

PowerShell Test-Path Cmdlet

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:

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:

Create A Directory If Not Already Exist In PowerShell:

Check If A File Exist In PowerShell:

Create A File If Not Already Exist In PowerShell:

Check If A Variable Is Declared In PowerShell:

Declare A Variable If Not Already Declared In PowerShell:

Check If A Registry Entry Exist In PowerShell:

Create A Registry Key If Not Already Exist In PowerShell:

Check If A Directory Or File Is Creates Before Or After Certain Time In PowerShell:

Remove A File If Already Exist In PowerShell:

Remove A Directory If Already Exist In PowerShell:

Remove A Registry Key If Already Exist In PowerShell:

Remove A Variable If Already Declared In PowerShell:

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

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply