Power Shell에서 Windows 바로 가기를 어떻게 따라갈 수 있습니까?


11

powershell을 사용하고 있으며 현재 디렉토리의 대상 디렉토리에 대한 바로 가기가 있습니다. 현재 디렉토리를 바로 가기가 가리키는 디렉토리로 변경하고 싶습니다. 논리적으로 내가하고 싶은 것은 :

cd your-files-here.lnk

그 지점을 넘어서십시오. 내가 대신 얻는 것은 :

Set-Location : Cannot find path 'your-files-here.lnk' because it does not exist.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\pscx\Modules\CD\Pscx.CD.psm1:111 char:17
+                 Set-Location <<<<  $path -UseTransaction:$UseTransaction
    + CategoryInfo          : ObjectNotFound: (your-files-here.lnk:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

나는 시도했다

ii your-files-here.lnk

그러나 현재 디렉토리를 변경하는 대신 탐색기 창이 열립니다.

답변:


6

이것을 Microsoft.PowerShell_profile.ps1파일에 추가 할 수 있습니다. 그러면 cd명령이 원하는대로 작동합니다.

remove-item alias:cd -force
function cd($target)
{
    if($target.EndsWith(".lnk"))
    {
        $sh = new-object -com wscript.shell
        $fullpath = resolve-path $target
        $targetpath = $sh.CreateShortcut($fullpath).TargetPath
        set-location $targetpath
    }
    else {
        set-location $target
    }
}

작동하려면 "$ fullpath = resolve-path $ target"을 "$ fullpath = (resolve-path $ target) .Path"로 변경해야했습니다.
— Omaer

2
또한 "cd"를 입력 한 후 비 디렉토리에 대해서는 PowerShell 자동 완성 기능이 작동하지 않습니다. 즉, .lnk 파일의 전체 이름을 직접 입력해야합니다.
— Omaer

8

불행히도 Windows는 바로 가기로 쉽게 작업 할 수 없습니다. 이것은 작동해야합니다 :

$sh = New-Object -COM WScript.Shell
cd $sh.CreateShortcut('your-files-here.lnk').TargetPath

대상이 일반적인 로컬 경로 인 경우에는 작동하지만 최소한 하나의 경우 바로 가기가 UNC 경로를 가리키고 있습니다. 이 경우 TargetPath가 비어 있습니다.
— Matthew Scouten

이상하게도 UNC 경로에 대한 .lnk 단축키가 있으며 .Targetpath 속성에 경로가 있습니다. 나는 하나를 가져 와서 목표 경로를 비우려고했는데 그렇게 저장하지 않았습니다.
— EBGreen

신경 쓰지 마. 나는 다른 일을 잘못하고 있었다. 잘 작동합니다.
— Matthew Scouten

미래 세대가 혜택을받을 수 있도록 무엇이 잘못되었는지 알려주십시오!
— uSlackr

1
한마디로 : CreateShortcut은 절대 경로를 원합니다.
— Matthew Scouten

3

지름길은 필수입니까?

이를 위해 Windows 링크를 사용할 수 있습니다. mklink /?Windows 링크 / 접합점에 대한 자세한 정보를 참조하십시오 .


나는 지름길을 결정한 사람이 아니다.
— Matthew Scouten

1
이 답변은 저에게 큰 도움이되었으며 솔루션이 기술적으로 (단축) 문제를 해결하지 못하면 원활하게 작동했습니다.
— twigmac
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.