Overview

Wait-Path will wait for a path to exist.  There are many situations where you want to ensure a path exists before proceeding.

This is simple enough to write on your own, but one of the benefits to PowerShell is that it is task based.  Rather than writing your own code to wait for a path to exist in every solution, you can provide a readable Wait-Path statement.

Instructions

PowerShell
Edit|Remove
# Download and dot source the function 
 . "\\Path\To\Wait-Path.ps1" 
 
# Get help for Wait-Path 
    Get-Help Wait-Path -Full 
 
# Wait for C:\Test to exist... timeout at 10 seconds 
    Wait-Path -Path C:\Test -Timeout 10 
 
# Create a test file, wait until we can see it, testing every .5 seconds, proceed 
    $TempFile = [System.IO.Path]::GetTempFileName() 
 
    if ( Wait-Path -Path $TempFile -Interval .5 -passthru ) 
    { 
        Set-Content -Path $TempFile -Value "Test!" 
    } 
    else 
    { 
        Throw "Could not find $TempFile" 
    } 
 
# We pass Path into Test-Path.  This means we can test other providers. 
    Wait-Path C:\Test, HKLM:\System

Examples

Wait for C:\ to exist, passthru the test result:

Wait for C:\Test to exist.  This fails.  Create the path, test again, and we succeed:

Create a temp file.  Wait until we can test the path for that file before taking action: