PowerShell/docs/new-symlink.md
2024-11-08 12:40:31 +01:00

2.3 KiB

The new-symlink.ps1 Script

This PowerShell script creates a new symbolic link file, linking to a target.

Parameters

/home/markus/Repos/PowerShell/scripts/new-symlink.ps1 [[-symlink] <String>] [[-target] <String>] [<CommonParameters>]

-symlink <String>
    Specifies the path to the new symlink file
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

-target <String>
    Specifies the path to the target
    
    Required?                    false
    Position?                    2
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./new-symlink.ps1 C:\User\Markus\Windows C:\Windows
 New symlink file 'C:\User\Markus\Windows' created, linking to: C:\Windows

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Creates a new symlink
.DESCRIPTION
	This PowerShell script creates a new symbolic link file, linking to a target.
.PARAMETER symlink
	Specifies the path to the new symlink file
.PARAMETER target
	Specifies the path to the target
.EXAMPLE
	PS> ./new-symlink.ps1 C:\User\Markus\Windows C:\Windows
	✅ New symlink file 'C:\User\Markus\Windows' created, linking to: C:\Windows
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$symlink = "", [string]$target = "")

try {
	if ($symlink -eq "" ) { $symlink = Read-Host "Enter new symlink filename" }
	if ($target -eq "" ) { $target = Read-Host "Enter path to target" }

	New-Item -path "$symlink" -itemType SymbolicLink -value "$target"

	"✅ New symlink file '$symlink' created, linking to: $target"
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}

(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:20)