PowerShell/docs/new-junction.md
2025-01-23 12:15:35 +01:00

2.3 KiB

The new-junction.ps1 Script

This PowerShell script creates a new junction, linking to a target.

Parameters

/Repos/PowerShell/scripts/new-junction.ps1 [[-junction] <String>] [[-target] <String>] [<CommonParameters>]

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

-target <String>
    Specifies the file 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-junction.ps1 D:\Win10 C:\Windows
 Created new junction 'D:\Windows' linking to: C:\Windows

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Create a junction
.DESCRIPTION
	This PowerShell script creates a new junction, linking to a target.
.PARAMETER junction
	Specifies the file path to the new junction
.PARAMETER target
	Specifies the file path to the target
.EXAMPLE
	PS> ./new-junction.ps1 D:\Win10 C:\Windows
	✅ Created new junction 'D:\Windows' linking to: C:\Windows
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

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

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

	New-Item -path "$symlink" -itemType Junction -value "$target"
	if ($lastExitCode -ne "0") { throw "Command 'New-Item' has failed" }

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

(page generated by convert-ps2md.ps1 as of 01/23/2025 12:15:23)