How to create a shortcut to a folder with PowerShell and Intune

RH-8125 111 Reputation points
2023-01-21T20:55:59.4266667+00:00

I've been battling this for the past several hours, searching to no avail; there are a couple old TN forum posts (tried to link here, but apparently that violates the community guidelines?) that don't answer the question and tell the questioner to ask in a different forum, but maddeningly there's neither a link to the question asked in that forum nor a hit while searching; I would reply there, but TN is of course now archived.

For posterity, here's my summary. While alternatives suggestions some offer are appreciated:

  • PowerShell inexplicably has no direct way to create a shortcut... it would save so much time if it did. . It can directly create a symbolic link, but 1) that requires admin rights, and 2) symbolic links behave differently.
  • Group Policy Preferences are great--but only if your machines are in office or routinely on a VPN.
  • PowerShell can use WScript.Shell to create shortcuts, but if you're trying to create a shortcut to a network folder, setting the TargetPath to that folder only works if the computer actually can reach that target folder when the script runs--in other words, the same issue as with Group Policy Preferences. PowerShell will create the shortcut, but the Target Type frustratingly will be a File rather than a File Folder (I found no info online how to control that; trailing slash or not doesn't matter).
  • The behavior of running these code suggestions varies depending whether you run this interactively or as a script.

There are many examples of scripts using WScript.Shell, but when someone asks about how to create a shortcut to a folder, the answers always seem to be either dodges ("Why are you doing that? Use symbolic links" or "Use Group Policy Preferences") or irrelevant examples of how to create a shortcut to a file.

Here's what I found does work: use Explorer.exe with an argument of the target path. By example:

$shell = New-Object -comObject WScript.Shell
$shortcut = $shell.CreateShortcut("[Target location of shortcut\shortcut name.lnk]")
$shortcut.TargetPath = "C:\Windows\Explorer.exe"
$shortcut.Arguments = """\\machine\share\folder"""
$shortcut.Save()

Hopefully this proves useful for someone else and saves some time!

Microsoft Intune
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
5,190 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,582 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Khaled Elsayed Mohamed 1,290 Reputation points
    2023-05-23T10:24:50.7566667+00:00

    Creating a shortcut to a folder on a user's device using Microsoft Intune requires configuring a custom PowerShell script and deploying it as a Win32 app. Here's a step-by-step guide on how to achieve this:

    1. Prepare the PowerShell script:

    Open a text editor and create a new PowerShell script. For example, you can name it "CreateShortcut.ps1".

    • Inside the script, add the following code:
    • powershell script:
    $ShortcutPath = "$env:PUBLIC\Desktop\ShortcutName.lnk" $TargetPath = "C:\Path\to\Target\Folder" $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($ShortcutPath) $Shortcut.TargetPath = $TargetPath $Shortcut.Save()
    
    

    Replace "ShortcutName.lnk" with the desired name for the shortcut file, and "C:\Path\to\Target\Folder" with the actual path of the folder you want to create a shortcut to.

     

    Save the PowerShell script.

     Package the PowerShell script as a Win32 app package:

     - Use the Microsoft Win32 Content Prep Tool (win32contentprep.exe) to package the PowerShell script as a Win32 app package. This tool is available in the IntuneWinAppUtil repository on GitHub: https://github.com/Microsoft/Microsoft-Win32-Content-Prep-Tool.

     

    • Follow the instructions provided in the tool's repository to package the PowerShell script. This involves running the IntuneWinAppUtil.exe command-line tool and specifying the PowerShell script and output folder as parameters.

     Upload the Win32 app package to Intune:

     - Log in to the Microsoft Endpoint Manager admin center (https://endpoint.microsoft.com) and navigate to "Apps" > "All apps".

     - Click on the "+ Add" button to add a new app.

     - Choose "Windows app (Win32)" as the app type.

     - Specify the required details for the app, including the name, description, publisher, and version.

     - On the "App package file" page, click on "Select file" and choose the Win32 app package (.intunewin file) generated in the previous step.

     - Complete the remaining steps in the wizard to finish uploading the app package.

     Configure the deployment settings:

     - In the app deployment settings, specify the desired behavior for the deployment, such as the user group(s) the app should be deployed to and the installation behavior.

     

    • You can also configure the detection rules to determine whether the app is already installed on the device.

     - Save the deployment settings.

     Assign the app to the desired group(s):

     - On the app's overview page, click on the "Assignments" tab.

     - Select the user group(s) to which you want to deploy the shortcut.

     - Save the assignments.

     Once the deployment is processed by Intune and the target devices sync with the Intune service, the PowerShell script will run on the user's devices and create the specified shortcut to the folder.

     

    1 person found this answer helpful.
    0 comments No comments

  2. Khaled Elsayed Mohamed 1,290 Reputation points
    2023-05-23T10:22:40.4066667+00:00

    To create a shortcut to a folder using PowerShell, you can utilize the New-Item cmdlet with the -ItemType parameter set to "SymbolicLink" or "Junction". Here's an example of how you can create a shortcut to a folder:

    the script:

    $targetFolder = "C:\Path\to\Target\Folder"
    
    $shortcutPath = "C:\Path\to\Shortcut.lnk"
    
    New-Item -ItemType SymbolicLink -Target $targetFolder -Path $shortcutPath
    
    

    In the above example, replace "C:\Path\to\Target\Folder" with the actual path of the folder you want to create a shortcut to. Similarly, replace "C:\Path\to\Shortcut.lnk" with the desired path and filename for the shortcut.

    By running the PowerShell script, a symbolic link (shortcut) will be created at the specified location, pointing to the target folder. You can then open the shortcut to access the target folder.

    Note that you might need administrative privileges to create a symbolic link in certain system-protected locations. Also, keep in mind that symbolic links are not the same as traditional shortcuts and may behave differently in some scenarios.

    If you prefer to create a directory junction instead of a symbolic link, simply change "SymbolicLink" to "Junction" in the -ItemType parameter.

    Make sure to run the PowerShell script with appropriate permissions and consider any specific requirements or restrictions within your environment.


    or:

    the script:

    $shortcut = New-Object -ComObject WScript.Shell
    
    $DesktopPath = [Environment]::GetFolderPath("Desktop")
    
    $ShortcutFile = "$DesktopPath\Documents.lnk"
    
    $shortcut.CreateShortcut($ShortcutFile)
    
    $shortcut.TargetPath = "C:\Users<username>\Documents"
    
    $shortcut. Save()
    
    

    Replace <username> with the username of the user who will be using the shortcut.


    In Intune, create a new configuration profile. In the "Profile Type" drop-down list, select "Custom". In the "Script" section, paste the PowerShell script that you created in step 1.

    Assign the configuration profile to the users who will be using the shortcut.

    Once the configuration profile is assigned, the shortcut will be created on the desktop of the users.

    Here are some additional things to keep in mind when creating a shortcut with PowerShell and Intune:

    • The PowerShell script must be saved with a .ps1 file extension.
    • The shortcut file must be saved with a .lnk file extension.
    • The target path of the shortcut must be a valid path on the user's device.
    • The configuration profile must be assigned to the users who will be using the shortcut.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.