none
Create Shortcut In My Network Places Using PowerShell

    Question

  • Hello!  How can one create a shortcut in My Network Places using PowerShell? I found a method described using VB at http://blogs.technet.com/heyscriptingguy/archive/2005/05/09/how-can-i-create-a-shortcut-in-my-network-places.aspx, but it does not create the shortcut in the same way that the native Windows GUI creates it.  Unfortunately, Group Policy Preferences does not help either since it does not create a proper shortcut in the NetHood folder (it simply creates a shortcut instead of creating a folder with target.lnk and desktop.ini). 

    I found another VB solution at http://bartdesmet.net/blogs/bart/archive/2005/09/23/3554.aspx that does correctly create the network location the same way the native Windows GUI does. Is there an easier method to accomplish this using PowerShell?

    Thanks.
    Tuesday, December 01, 2009 6:37 AM

Answers

  • I finally started to get my feet wet with PowerShell and quickly scrapped together a new PowerShell script to accomplish this. In case this would help someone else, here is the script to add a location into the My Network Places folder just like the way the native Windows GUI produces this (I used some code from another forum post you did Shay - thanks!):

    #References:
    #http://social.technet.microsoft.com/Forums/en/ITCG/thread/bd00d576-6096-4c8d-bd82-3c5853eb6675
    #http://blogs.technet.com/heyscriptingguy/archive/2007/07/12/how-can-i-use-windows-powershell-to-retrieve-environment-variables-and-special-folder-paths.aspx
    #http://technet.microsoft.com/en-us/library/ee176914.aspx
    #http://thepowershellguy.com/blogs/posh/archive/2007/02/08/sesame-powershell-scripting-text-files.aspx
    #http://technet.microsoft.com/en-us/library/ee176949.aspx
    #http://blogs.technet.com/heyscriptingguy/archive/2009/08/25/hey-scripting-guy-can-you-translate-this-vbscript-to-windows-powershell.aspx
    #http://www.vistax64.com/powershell/103072-changing-target-path-windows-shortcuts.html
    #http://social.technet.microsoft.com/Forums/en/ITCG/thread/315f9803-dd42-4e44-86a4-77726306ab09
    
    cls
    
    $NetLocName = $args[0]
    $NetLocTargetPath = $args[1]
    
    if ( !(test-Path $NetLocTargetPath) ) # -pathType container) )
    {
    	write-Host "Problem with inputted target path: " $NetLocTargetPath
    	write-Host "Exiting..."
    	exit
    }
    
    $const_NETHOOD = 0x13
    $oApp = new-Object -com Shell.Application
    $oShell = new-Object -com Wscript.Shell
    
    $NetHood = ($oApp.Namespace($const_NETHOOD)).Self.Path
    $NetLocLocalPath = "$NetHood\$NetLocName"
    
    if ( !(test-Path $NetLocLocalPath -pathType container) )
    {
    	new-Item $NetLocLocalPath -type directory -force
    	attrib "$NetLocLocalPath" +R
    }
    
    if ( !(test-Path "$NetLocLocalPath\Desktop.ini" -pathType leaf) )
    {
    	$oFile = new-Item "$NetLocLocalPath\Desktop.ini" -type file -force
    	add-Content $oFile "[.ShellClassInfo]"
    	add-Content $oFile "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}"
    	add-Content $oFile "Flags=2"
    	attrib "$NetLocLocalPath\Desktop.ini" +H +S -A
    }
    
    
    if ( (test-Path "$NetLocLocalPath\target.lnk" -pathType leaf) )
    {
    	$oLnk = $oShell.CreateShortcut("$NetLocLocalPath\target.lnk")
    	$OldLnk = $oLnk.TargetPath
    
    	if ($OldLnk -ne $NetLocTargetPath)
    	{
    		$oLnk.TargetPath = $NetLocTargetPath
    		$oLnk.Save()
    	}
    }
    else
    {
    	$oLnk = $oShell.CreateShortcut("$NetLocLocalPath\target.lnk")
    
    	$oLnk.TargetPath = $NetLocTargetPath
    	$oLnk.Save()
    }
    

    Monday, December 28, 2009 4:10 PM

All replies

  • PowerShell uses the same com objects as WScript:
     
    $Const_NETHOOD = 0x13
    $oShell = new-object -com Wscript.Shell
    $oApp = new-object -com Shell.Application
    $oFolder = $oApp.Namespace($Const_NETHOOD)
    $oFolderItem = $oFolder.Self
    $NetHood = $oFolderItem.Path
    $shortcutName = "Shortcut Name"
    $shortcutPath = "\\server\share"
    $oShortcut = $oShell.CreateShortcut("$NetHood\$shortcutName.lnk")
    $oShortcut.TargetPath = $shortcutPath
    $oShortcut.Save()

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Tuesday, December 01, 2009 1:11 PM
    Moderator
  • Hello Shay!  Thanks for you quick response.  It appears that this only creates a .lnk file.  However, the native Windows method produces a read only folder that contains target.lnk and desktop.ini.  Here is an example for a My Network Place called "test":

       SH      <NetHood folder>\test\Desktop.ini
    A          <NetHood folder>\test\target.lnk
         R     <NetHood folder>\test

    The desktop.ini file contains:
    [.ShellClassInfo]
    CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
    Flags=2

    There is a script to create this at http://bartdesmet.net/blogs/bart/archive/2005/09/23/3554.aspx (scroll down to "Wednesday, October 19, 2005 2:02 PM by bart"), but I am wondering if there is a simpler way using PowerShell.

    Thanks.
    Tuesday, December 01, 2009 4:15 PM
  • I finally started to get my feet wet with PowerShell and quickly scrapped together a new PowerShell script to accomplish this. In case this would help someone else, here is the script to add a location into the My Network Places folder just like the way the native Windows GUI produces this (I used some code from another forum post you did Shay - thanks!):

    #References:
    #http://social.technet.microsoft.com/Forums/en/ITCG/thread/bd00d576-6096-4c8d-bd82-3c5853eb6675
    #http://blogs.technet.com/heyscriptingguy/archive/2007/07/12/how-can-i-use-windows-powershell-to-retrieve-environment-variables-and-special-folder-paths.aspx
    #http://technet.microsoft.com/en-us/library/ee176914.aspx
    #http://thepowershellguy.com/blogs/posh/archive/2007/02/08/sesame-powershell-scripting-text-files.aspx
    #http://technet.microsoft.com/en-us/library/ee176949.aspx
    #http://blogs.technet.com/heyscriptingguy/archive/2009/08/25/hey-scripting-guy-can-you-translate-this-vbscript-to-windows-powershell.aspx
    #http://www.vistax64.com/powershell/103072-changing-target-path-windows-shortcuts.html
    #http://social.technet.microsoft.com/Forums/en/ITCG/thread/315f9803-dd42-4e44-86a4-77726306ab09
    
    cls
    
    $NetLocName = $args[0]
    $NetLocTargetPath = $args[1]
    
    if ( !(test-Path $NetLocTargetPath) ) # -pathType container) )
    {
    	write-Host "Problem with inputted target path: " $NetLocTargetPath
    	write-Host "Exiting..."
    	exit
    }
    
    $const_NETHOOD = 0x13
    $oApp = new-Object -com Shell.Application
    $oShell = new-Object -com Wscript.Shell
    
    $NetHood = ($oApp.Namespace($const_NETHOOD)).Self.Path
    $NetLocLocalPath = "$NetHood\$NetLocName"
    
    if ( !(test-Path $NetLocLocalPath -pathType container) )
    {
    	new-Item $NetLocLocalPath -type directory -force
    	attrib "$NetLocLocalPath" +R
    }
    
    if ( !(test-Path "$NetLocLocalPath\Desktop.ini" -pathType leaf) )
    {
    	$oFile = new-Item "$NetLocLocalPath\Desktop.ini" -type file -force
    	add-Content $oFile "[.ShellClassInfo]"
    	add-Content $oFile "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}"
    	add-Content $oFile "Flags=2"
    	attrib "$NetLocLocalPath\Desktop.ini" +H +S -A
    }
    
    
    if ( (test-Path "$NetLocLocalPath\target.lnk" -pathType leaf) )
    {
    	$oLnk = $oShell.CreateShortcut("$NetLocLocalPath\target.lnk")
    	$OldLnk = $oLnk.TargetPath
    
    	if ($OldLnk -ne $NetLocTargetPath)
    	{
    		$oLnk.TargetPath = $NetLocTargetPath
    		$oLnk.Save()
    	}
    }
    else
    {
    	$oLnk = $oShell.CreateShortcut("$NetLocLocalPath\target.lnk")
    
    	$oLnk.TargetPath = $NetLocTargetPath
    	$oLnk.Save()
    }
    

    Monday, December 28, 2009 4:10 PM
  • Tuesday, December 29, 2009 7:47 AM
    Moderator
  • 
    
    

    Shah,

    Help, little new to poweshell, where would I put the variables like name and path for the script to work. Would this work with Window XP, SP3.

    Thanks,

    Baraan12

    Thursday, December 13, 2012 1:55 PM
  • Baraan12: Sorry, I don't recall receiving an alert that this thread was updated.  To answer your question in case you still need an answer, you supply the name and UNC path as parameters to the script.  So for example, if you are trying to create a new Network Location called "Images" to a UNC path \\server\Images, you would do something like this:

    .\Script.ps1 "Images" "\\server\Images"

    This is because in the code, PowerShell places the passed parameters into a special array called $args and hence we are able to grab the passed in parameters from this location.

    Good luck!

    Saturday, June 29, 2013 12:35 AM
  • Thank you thank you thank you!!!!

    Been trying to get something like this for 2 days!

    Thursday, May 15, 2014 5:44 PM
  • Thanks to the info in this post I found another solution that does not require a script:

    1. Create a folder:
      GPO>User configuration>Preferences>Windows Settings>Folders
      Path: %NetPlacesDir%\<name of the share>

      You might want to set "Remove this item when it is no longer applied" check box on "common tab"
    2. Create a link:
      GPO>User configuration>Preferences>Windows Settings>Shortcuts
      Name:                  <name of the share>\target
      Target Type:       File System Object
      Location:              My network Places
      Target path:       \\server\shares\share
    3. Create Desktop.ini:
      GPO>User configuration>Preferences>Windows Settings>Ini Files

      File Path: %NetPlacesDir%\<name of the share>\\Desktop.ini
      Section Name:  .ShellClassInfo
      Property Name:  CLSID2
      Property Value: {0AFACED1-E828-11D1-9187-B532F1E9575D}

      File Path: %NetPlacesDir%\<name of the share>\\Desktop.ini
      Section Name:  .ShellClassInfo
      Property Name:  Flags
      Property Value:  2
    Tuesday, March 24, 2015 9:05 PM
  • Hi Aakash,

    Thank you for your Post . It is really useful. 
    Do you know how we can map a CLSID as Target Path. For example , How to map the CLSID of myComputer as a network location from powershell?

    Wednesday, August 31, 2016 6:20 AM
  • When creating the Folder you must set the Attributes to Read-Only or the link will not work.

    • Edited by Dsmith512 Wednesday, November 02, 2016 4:26 PM
    Wednesday, November 02, 2016 3:40 PM