Skip to content

Instantly share code, notes, and snippets.

@PSingletary
Last active November 16, 2024 08:16
Show Gist options
  • Save PSingletary/cdf3f70d4b9625e22a4e91f9f8f450ae to your computer and use it in GitHub Desktop.
Save PSingletary/cdf3f70d4b9625e22a4e91f9f8f450ae to your computer and use it in GitHub Desktop.
Powershell Function to create a Network Location based on a name and a target path
<#
.SYNOPSIS
create a Network Location based on a name and a target path
.DESCRIPTION
create a Network Location based on a name and a target path
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.NOTES
Version: 1.0
Author: Chris O'Prey
Creation Date: Wednesday, April 14, 2010 11:23 AM
Purpose/Change: Initial script development
.REFERENCE
Add a Network Location via Script - https://social.technet.microsoft.com/Forums/scriptcenter/en-US/fd34260e-ee4c-47c5-8c69-872a5239745f/add-a-network-location-via-script?forum=ITCG
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
function Add-NetworkLocation
{
param(
[string]$name,
[string]$targetPath
)
# Get the basepath for network locations
$shellApplication = New-Object -ComObject Shell.Application
$nethoodPath = $shellApplication.Namespace(0x13).Self.Path
# Only create if the local path doesn't already exist & remote path exists
if ((Test-Path $nethoodPath) -and !(Test-Path "$nethoodPath\$name") -and (Test-Path $targetPath))
{
# Create the folder
$newLinkFolder = New-Item -Name $name -Path $nethoodPath -type directory
# Create the ini file
$desktopIniContent = @"
[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Flags=2
ConfirmFileOp=1
"@
$desktopIniContent | Out-File -FilePath "$nethoodPath\$name\Desktop.ini"
# Create the shortcut file
$shortcut = (New-Object -ComObject WScript.Shell).Createshortcut("$nethoodPath\$name\target.lnk")
$shortcut.TargetPath = $targetPath
$shortcut.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85"
$shortcut.Description = $targetPath
$shortcut.WorkingDirectory = $targetPath
$shortcut.Save()
# Set attributes on the files & folders
Set-ItemProperty "$nethoodPath\$name\Desktop.ini" -Name Attributes -Value ([IO.FileAttributes]::System -bxor [IO.FileAttributes]::Hidden)
Set-ItemProperty "$nethoodPath\$name" -Name Attributes -Value ([IO.FileAttributes]::ReadOnly)
}
}
@muscanto
Copy link

Any way you could update this to include a removal function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment