Search code examples
powershellpathappveyor

PowerShell: Add every sub-directory to path


On Windows 10 and via PowerShell, how do you add every sub-directory to the PATH variable?

I fine some method for Linux (e.g., this one), and "adapting" them to PowerShell, I came across the following.

'$env:PATH = $env:PATH + (find C:\Users\appveyor\ -type d -printf "\";%pbuild\\"")'

This throws The filename, directory name, or volume label syntax is incorrect.

A bit more context: I am running this on appveyor, and trying to add every path under appveyor (simplified for clarity) such as C:\Users\appveyor\*\build\ to path.


Solution

  • $env:PATH = 
        @($env:PATH) + 
        [string]::join(
            ";", 
            (Get-ChildItem -LiteralPath "[PATH TO SEARCH]" -Directory -Recurse).fullname
        )
    

    This based on @briantist's suggestion; the main difference is in the join syntax.

    To get this work on appveyor, do the following:

    {ps:
        "$env:PATH = 
             @($env:PATH) + [string]::join(
                 \";\", 
                 (Get-ChildItem -LiteralPath \"[PATH TO SEARCH]\" -Directory -Recurse).fullname
        )"
    }