Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

Powershell script to fill out Start in section with path


Trying to get powershell to fill out the "Start in" section within the shortcut file via:

$Shortcut.WorkingDirectory

Script so far:

$file = pwd
# Create a Shortcut with Windows PowerShell
$TargetFile = "$file\file.vbs"
$ShortcutFile = "$file\file.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.WorkingDirectory = $file

This is the error i receive.

Exception setting "WorkingDirectory": Cannot convert the "C:\PATH\TO\FOLDER" value of type "PathInfo" to type "string".
At line:8 char:5
+     $Shortcut.WorkingDirectory = $file
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : RuntimeException
    $Shortcut.Save()

Help would be appreciated. :)


Solution

  • pwd is an alias for command Get-Location. It returns a PathInfo object with many properties that points to the current directory. The Path property contains the path string.

    Therefore, you need to access that Path property from the pwd output or from $file:

    $file = (pwd).Path
    $TargetFile = "$file\file.vbs"
    $ShortcutFile = "$file\file.lnk"
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $TargetFile
    $Shortcut.WorkingDirectory = $file
    

    The automatic variable $pwd also contains the PathInfo object with many properties that points to the current directory. You can access the same value using $pwd.Path:

    $file = $pwd.Path
    

    Additional Info:

    You can view properties and type information using the Get-Member command:

    $pwd | Get-Member
    
       TypeName: System.Management.Automation.PathInfo
    
    Name         MemberType Definition
    ----         ---------- ----------
    Equals       Method     bool Equals(System.Object obj)
    GetHashCode  Method     int GetHashCode()
    GetType      Method     type GetType()
    ToString     Method     string ToString()
    Drive        Property   System.Management.Automation.PSDriveInfo Drive {get;}
    Path         Property   string Path {get;}
    Provider     Property   System.Management.Automation.ProviderInfo Provider {get;}
    ProviderPath Property   string ProviderPath {get;}
    

    Each property value can be accessed directly using the member access operator . with the syntax object.Property. The Select-Object -ExpandProperty Property syntax is also a popular way to retrieve a property value.

    # Member Access Method
    $pwd.Path
    C:\MyCurrentPath
    
    # Select-Object Method
    
    $pwd | Select-Object -ExpandProperty Path
    C:\MyCurrentPath
    

    PathInfo objects contain a ToString() override method that returns the path as a string.

    $pwd.ToString()
    C:\MyCurrentPath
    
    (pwd).ToString()
    C:\MyCurrentPath