CDCompletion.ps1

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

function CompleteCdPath{    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
     
     
     if ($commandAst.CommandElements.Count -ne 2)
     {
        return
     }
      $cdpath = @('.') 
    if (Test-Path '~\documents\WindowsPowershell\cdpath.txt') 
    { 
      $cdpath += Get-CDPath     
    }     

     $ce = $commandAst.CommandElements[1]
     if ($ce -isnot [StringConstantExpressionAst])
     {
        return
     }
     $pattern = $commandAst.CommandElements[1].Value             
     
     
     [CompletionCompleters]::CompleteFilename("$wordToComplete*") | Where-Object ResultType -eq ProviderContainer
     
     Join-Path $cdpath "$pattern*" -Resolve -ea:SilentlyContinue | foreach {        
        if (Test-Path -PathType Container $_)
        {
            if ($_.Contains(' ')){
                $_ = "'$_'"
            }
            [CompletionResult]::new($_,$_, [CompletionResultType]::ProviderContainer,  $_)
        }
     }
}


function CompleteCdRemaining{    
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
               
    $cdpath = @('.') 
    if (Test-Path '~\documents\WindowsPowershell\cdpath.txt') 
    { 
      $cdpath += Get-CDPath     
    }      

     $ce = $commandAst.CommandElements[1]
     if ($ce -isnot [StringConstantExpressionAst])
     {
        return
     }
     $el = $commandAst.CommandElements
     $count = ($el.Count - 1)
     if ($wordToComplete){
        $count--
     }
     if($count -eq 1){
        $pattern = $el[1].Value + "*\$wordtoComplete*"
     }
     else{
        $pattern = $el[1..$count].Value -join '*\' 
        $pattern += "*\$wordtoComplete*"
     }
     
     
     [CompletionCompleters]::CompleteFilename("$wordToComplete*") | Where-Object ResultType -eq ProviderContainer
     
     Join-Path $cdpath $pattern -Resolve -ea:SilentlyContinue | foreach {        
        if (Test-Path -PathType Container $_)
        {
            $name = split-path -leaf $_
            if ($name.Contains(' ')){
                $name = "'$name'"
            }
            [CompletionResult]::new($name,$name, [CompletionResultType]::ProviderContainer,  $_)
            
        }
     }
}


if (Get-Command Register-ArgumentCompleter -ErrorAction:SilentlyContinue){    
    
    Register-ArgumentCompleter -CommandName Set-CDPathLocation -ParameterName Path -ScriptBlock $function:CompleteCdPath
    Register-ArgumentCompleter -CommandName Set-CDPathLocation -ParameterName Remaining -ScriptBlock $function:CompleteCdRemaining
} 
 
# SIG # Begin signature block
# MIINLAYJKoZIhvcNAQcCoIINHTCCDRkCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUqPGiIzNpMwqapG2RJrwm8Dlq
# lXKgggpuMIIFMDCCBBigAwIBAgIQBAkYG1/Vu2Z1U0O1b5VQCDANBgkqhkiG9w0B
# AQsFADBlMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYD
# VQQLExB3d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVk
# IElEIFJvb3QgQ0EwHhcNMTMxMDIyMTIwMDAwWhcNMjgxMDIyMTIwMDAwWjByMQsw
# CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cu
# ZGlnaWNlcnQuY29tMTEwLwYDVQQDEyhEaWdpQ2VydCBTSEEyIEFzc3VyZWQgSUQg
# Q29kZSBTaWduaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
# +NOzHH8OEa9ndwfTCzFJGc/Q+0WZsTrbRPV/5aid2zLXcep2nQUut4/6kkPApfmJ
# 1DcZ17aq8JyGpdglrA55KDp+6dFn08b7KSfH03sjlOSRI5aQd4L5oYQjZhJUM1B0
# sSgmuyRpwsJS8hRniolF1C2ho+mILCCVrhxKhwjfDPXiTWAYvqrEsq5wMWYzcT6s
# cKKrzn/pfMuSoeU7MRzP6vIK5Fe7SrXpdOYr/mzL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