PowerShell/docs/new-junction.md
2025-05-12 22:04:02 +02:00

91 lines
2.5 KiB
Markdown

The *new-junction.ps1* Script
===========================
This PowerShell script creates a new junction, linking to a target folder. The target folder can reside on another disk.
Parameters
----------
```powershell
/Repos/PowerShell/scripts/new-junction.ps1 [[-junction] <String>] [[-targetDir] <String>] [<CommonParameters>]
-junction <String>
Specifies the path and filename of the new junction
Required? false
Position? 1
Default value
Accept pipeline input? false
Aliases
Accept wildcard characters? false
-targetDir <String>
Specifies the path to the target directory
Required? false
Position? 2
Default value
Accept pipeline input? false
Aliases
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./new-junction.ps1 C:\User\Joe\D_drive D:
New junction 'C:\User\Joe\D_drive' created, linking to: 📂D:
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Create a junction
.DESCRIPTION
This PowerShell script creates a new junction, linking to a target folder. The target folder can reside on another disk.
.PARAMETER junction
Specifies the path and filename of the new junction
.PARAMETER targetDir
Specifies the path to the target directory
.EXAMPLE
PS> ./new-junction.ps1 C:\User\Joe\D_drive D:
✅ New junction 'C:\User\Joe\D_drive' created, linking to: 📂D:
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$junction = "", [string]$targetDir = "")
try {
if ($junction -eq "" ) { $junction = Read-Host "Enter the new junction's path and filename" }
if ($targetDir -eq "" ) { $target = Read-Host "Enter the path to the target directory " }
New-Item -path "$junction" -itemType Junction -value "$targetDir"
if ($lastExitCode -ne 0) { throw "Command 'New-Item' has failed" }
"✅ New junction '$junction' created, linking to: 📂$targetDir"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
*(page generated by convert-ps2md.ps1 as of 05/12/2025 22:02:56)*