0

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

2

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

@postanote I understood the question, this command will not create a new lnk, only an object so it can read the properties of the existing lnk
OK, OPP (old people problems), where the brain is feeding in details that are not there. ;-}. It happens to us old dudes. ;-}
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.