Home > Net >  How do I open a folder link in powershell/windows terminal?
How do I open a folder link in powershell/windows terminal?

Time:01-03

When I type the obvious thing

  PS > cd folderName.lnk

or

 PS > cd folderName.lnk

in both cases it claims that the file doesn't exist, even though 'ls' shows that it does. Any suggestions? Thank you

CodePudding user response:

try this:

$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('<full-path-to-shortcut>').TargetPath

set-location -LiteralPath $target

make sure you use the full path to the shortcut, not a relative path like .\FolderName.lnk

you can also script it so you always get the absolute path:

$shortcut = '.\Shortcut.lnk'
$absolutepath = Convert-Path -Path $shortcut

$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut($absolutepath).TargetPath

Set-Location -LiteralPath $target

CodePudding user response:

As noted by others, .lnk files are not directories,...

Get-ChildItem -Path "$env:USERPROFILE\Desktop"
# Results
<#
    Directory: C:\Users\WDAGUtilityAccount\Desktop


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          1/2/2023   1:39 PM            737 Scripts.lnk  
#>

...so you cannot directly navigate to them with any directory-like command. The Desktop is the folder for that file.

(Get-ChildItem -Path "$env:USERPROFILE\Desktop").Fullname
# Results
<#
C:\Users\WDAGUtilityAccount\Desktop\Scripts.lnk
#>

Get-ChildItem -Path "$env:USERPROFILE\Desktop" | 
Select-Object -Property '*'
# Results
<#
PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Users\WDAGUtilityAccount\Desktop\Scripts.lnk
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\Users\WDAGUtilityAccount\Desktop
PSChildName       : Scripts.lnk
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             C:\Users\WDAGUtilityAccount\Desktop\Scripts.lnk
                    InternalName:     
                    OriginalFilename: 
                    FileVersion:      
                    FileDescription:  
                    Product:          
                    ProductVersion:   
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:         
                    
BaseName          : Scripts
Target            : {}
LinkType          : 
Name              : Scripts.lnk
Length            : 737
DirectoryName     : C:\Users\WDAGUtilityAccount\Desktop
Directory         : C:\Users\WDAGUtilityAccount\Desktop
IsReadOnly        : False
Exists            : True
FullName          : C:\Users\WDAGUtilityAccount\Desktop\Scripts.lnk
Extension         : .lnk
CreationTime      : 1/2/2023 1:39:41 PM
CreationTimeUtc   : 1/2/2023 9:39:41 PM
LastAccessTime    : 1/2/2023 1:39:43 PM
LastAccessTimeUtc : 1/2/2023 9:39:43 PM
LastWriteTime     : 1/2/2023 1:39:41 PM
LastWriteTimeUtc  : 1/2/2023 9:39:41 PM
Attributes        : Archive
#>

So, you have to extract that FQDN from the link and use the available directory/Name commands to navigate to that path.

  • Related