October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check Whether a File or Folder Exists in PowerShell

Use PowerShell Test-Path to check for an existing path before acting. Learn when to choose -LiteralPath, wildcard-aware -Path, and Leaf or Container checks.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Test-Path in an if statement to check whether a path exists before acting on it. For an exact path stored in a variable, prefer -LiteralPath; add -PathType Leaf for a file or -PathType Container for a directory.

Check a path before running a command

Test-Path returns $true when all elements of a path exist and $false when any are missing. Use that Boolean result directly to choose what a script does:

As an Amazon Associate I earn from qualifying purchases.

$path = 'C:Reportstoday.csv'
if (Test-Path -LiteralPath $path -PathType Leaf) {
    Import-Csv -LiteralPath $path
}
else {
    Write-Warning "File not found: $path"
}

Here, -LiteralPath tests the exact value in $path, and -PathType Leaf requires the target to be a file rather than merely some existing path. Microsoft documents this behavior in its Windows PowerShell 5.1 Test-Path reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose -LiteralPath or -Path

The key difference is whether wildcard characters should be interpreted:

Parameter Use it when How it treats the value
-LiteralPath You have an exact path, especially one from a variable or user input. Uses the value as typed; wildcard characters such as [ and ] are not interpreted.
-Path You intend to match a wildcard pattern. Can interpret wildcard characters. Provider and filter syntax may vary by provider.

For example, use Test-Path -Path 'C:Reports*.csv' when a wildcard match is intended. Use Test-Path -LiteralPath $path when $path should be treated as one exact name.

Require a file or directory

A plain existence check does not say whether the target is the kind of item your next command expects. Specify -PathType to narrow the test:

Rank #2
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback
  • -PathType Leaf tests for a file-like terminal item.
  • -PathType Container tests for a directory or other container.

For example, check a folder with Test-Path -LiteralPath $folder -PathType Container. These path types are documented in Microsoft’s Windows PowerShell 5.1 reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Existence is different from valid syntax

-IsValid checks whether path syntax is valid; it does not establish that the path exists. A syntactically valid path can still point to a missing item. Use Test-Path without -IsValid when the question is whether the target is present.

Handle empty and null input

An empty or whitespace-only string returns $false. By contrast, the documented behavior for $null, an array of nulls, or an empty array is a non-terminating error. If a path comes from optional caller input, validate that input before passing it to Test-Path.

Remember that paths can use other providers

Test-Path works with data exposed through PowerShell providers, not only filesystem locations. A PowerShell path can refer to provider data such as the registry. Choose the path and wildcard behavior with the active provider in mind.

Account for version-specific behavior

Microsoft publishes separate references for Windows PowerShell 5.1 and PowerShell 7.6. Check the reference that matches the installed release, especially when using date filters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • In PowerShell 7.5 and later, -NewerThan and -OlderThan can be used with any -PathType value to test a date range and the age of directories.
  • Before PowerShell 7.5, -NewerThan was ignored with -PathType values other than Any; -OlderThan was ignored when used with -NewerThan.
  • The PowerShell 7.6 reference also records historical behavior through PowerShell 6.1.2 in which combining -IsValid and -PathType caused -PathType to be ignored. Avoid relying on that combination without checking the documentation for your release.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A path check is not a guarantee of success

Test-Path reports the path state at the moment the check runs. The item can change before a later command acts, and existence alone does not guarantee that the later operation has permission to succeed. Keep appropriate error handling around operations that can fail because of permissions, concurrent changes, or other I/O conditions.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.