Skip to content

Instantly share code, notes, and snippets.

@asears
Last active April 11, 2026 12:30
Show Gist options
  • Select an option

  • Save asears/7f821c5ebb56bdb256a37b78565f193f to your computer and use it in GitHub Desktop.

Select an option

Save asears/7f821c5ebb56bdb256a37b78565f193f to your computer and use it in GitHub Desktop.
Gets working folder path - all the cli tool things
Get-Command pac | Format-List
Get-Command dotnet | Format-List
Get-Command code | Format-List
(Get-Command notepad).Path
$exeName = "pac"
$pathDirs = [System.Environment]::GetEnvironmentVariable("PATH").Split(';') |
            Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
foreach ($dir in $pathDirs) {
    
    $fullPath = Join-Path -Path $dir -ChildPath $exeName
    if (Test-Path $fullPath) {
        Write-Output $fullPath
    }
}
$exeName = "code"
$pathDirs = [System.Environment]::GetEnvironmentVariable("PATH").Split(';') |
            Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
            Select-Object -Unique

# Extensions we care about
$extensions = '.exe', '.cmd', '.bat', '.ps1', '.com', '.lnk'

foreach ($dir in $pathDirs) {

    $fullPath = Join-Path -Path $dir -ChildPath $exeName

    if (Test-Path $fullPath -PathType Leaf) {
        Write-Output "Found: $fullPath"
        Write-Output "Files in same directory:"

        $all = Get-ChildItem -Path $dir -File -ErrorAction SilentlyContinue |
               Where-Object {
                   # include normal executable/script extensions
                   $extensions -contains $_.Extension -or
                   # include shim launchers (no extension)
                   $_.Extension -eq ''
               }

        if ($all) {
            $all | Select-Object -ExpandProperty FullName
        } else {
            Write-Output "No matching files found in this directory."
        }

        Write-Output ""
    }
}
$exeName = "code"

# Feature toggles
$IncludeSubfolders = $true
$IncludeParentFolders = $true

# Allowed extensions
$extensions = '.exe', '.cmd', '.bat', '.ps1', '.com', '.lnk'

# Application root boundaries
$rootBoundaries = @(
    "C:\Program Files",
    "C:\Program Files (x86)",
    "$env:LOCALAPPDATA",
    "$env:APPDATA"
)

# Clean PATH
$pathDirs = [System.Environment]::GetEnvironmentVariable("PATH").Split(';') |
            Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
            Select-Object -Unique

function Get-ExecutableFilesInFolder {
    param(
        [string]$Folder
    )

    $items = Get-ChildItem -Path $Folder -File -ErrorAction SilentlyContinue

    $items | Where-Object {
        # normal executable/script extensions
        $extensions -contains $_.Extension -or
        # shim launchers (no extension)
        $_.Extension -eq ''
    }
}

foreach ($dir in $pathDirs) {

    $fullPath = Join-Path -Path $dir -ChildPath $exeName

    if (Test-Path $fullPath -PathType Leaf) {

        Write-Output "Found: $fullPath"
        Write-Output "Scanning executable context..."

        # Always start in the folder where the file was found
        $scanTargets = @($dir)

        # Add subfolders if enabled
        if ($IncludeSubfolders) {
            $subdirs = Get-ChildItem -Path $dir -Directory -Recurse -ErrorAction SilentlyContinue
            $scanTargets += $subdirs.FullName
        }

        # Add parent folders if enabled, but stop at boundaries
        if ($IncludeParentFolders) {
            $parent = Split-Path $dir -Parent
            while ($parent -and ($rootBoundaries -notcontains $parent)) {
                $scanTargets += $parent
                $parent = Split-Path $parent -Parent
            }
        }

        # Remove duplicates
        $scanTargets = $scanTargets | Select-Object -Unique

        # Collect results
        $results = foreach ($folder in $scanTargets) {
            Get-ExecutableFilesInFolder -Folder $folder
        }

        # Remove the matched file itself from the list
        $results = $results | Where-Object { $_.FullName -ne $fullPath }

        if ($results) {
            $results | Select-Object -ExpandProperty FullName
        } else {
            Write-Output "No executables found in application folders."
        }

        Write-Output ""
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment