1

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.

briantist
  • 39,669
  • 6
  • 64
  • 102
Hamed
  • 1,460
  • 1
  • 16
  • 32
  • That has been answered before. You could have searched before you asked. [https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables](https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables) ... take look at the answer of hoge – Olaf Jun 02 '20 at 00:59
  • But this is a different question; this is not about setting the env PATH, it is rather about using `FIND` properly in concert with setting the PATH. – Hamed Jun 02 '20 at 01:04

2 Answers2

2

The find command on Linux is very different from the Find command on Windows.

In your case, I would use Get-ChildItem in PowerShell (which can also be used via the aliases gci, dir, ls), to return an array of directory objects, added to the current path casted as an array, and then -join them with the Windows path separator, which is a semicolon ;.

That might look like this:

$env:PATH = (
    @($env:PATH) + (
        Get-ChildItem -LiteralPath 'C:\Users\appveyor' -Directory -Recurse
    )
) -join [System.IO.Path]::PathSeparator # <- that could just be a literal string ';'
briantist
  • 39,669
  • 6
  • 64
  • 102
  • Thanks for the suggestion; however, I still get the `The filename, directory name, or volume label syntax is incorrect.` error. – Hamed Jun 02 '20 at 01:12
0
$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
    )"
}
Hamed
  • 1,460
  • 1
  • 16
  • 32