Link.psm1
#region New-HardLink function New-HardLink() { <# .SYNOPSIS Function used to create a hard link. .DESCRIPTION Function used to create a hard link. Target directory must exist. Command requires administrator privileges. .EXAMPLE New-HardLink -Path newdir -Target .\dir This command will create a hard link with name newdir that points to the dir directory on the current folder. #> Param( [Parameter(Position = 0, Mandatory = $true)] [string] $Path, [Parameter(Mandatory = $true)] [string] $Target ) # Test if console has administrator priviledges. if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Test if target exists and is a file if( Test-Path $Target) { if( -Not (Test-Path $Target -PathType Leaf) ) { Throw "Target must be a file." } } else { Throw "Could not find target path" } # Get the full path to the target $t = Resolve-Path $Target # Create the link $s = cmd /c mklink /h $Path $t.Path FormNewLinkResult $s "Hard" } #endregion #region New-JunctionLink function New-JunctionLink() { <# .SYNOPSIS Function used to create a junction link. .DESCRIPTION Function used to create a junction link. Target directory must exist. Command requires administrator privileges. .EXAMPLE New-JunctionLink -Path newdir -Target .\dir This command will create a junction link with name newdir that points to the dir directory on the current folder. #> Param( [Parameter(Position = 0, Mandatory = $true)] [string] $Path, [Parameter(Mandatory = $true)] [string] $Target ) # Check if the console has administrator privileges. if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Test if target exists and is a directory if( Test-Path $Target ) { if( Test-Path $Target -PathType Leaf ) { Throw "Target must be a directory." } } else { Throw "Could not find target path" } # Get the full path of the target $t = Resolve-Path $Target # Create the link $s = cmd /c mklink /j $Path $t.Path FormNewLinkResult $s "Junction" } #endregion #region New-SymbolicLink function New-SymbolicLink() { <# .SYNOPSIS Function used to create a symbolic link. .DESCRIPTION Function used to create a symbolic link. Target directory must exist. Command requires administrator privileges. .EXAMPLE New-SymbolicLink -Path newdir -Target .\dir This command will create a symbolic link with name newdir that points to the dir directory on the current folder. #> Param( [Parameter(Position = 0, Mandatory = $true)] [string] $Path, [Parameter(Mandatory = $true)] [string] $Target ) # Check if the console has administrator privileges if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Test if target exists and is a directory if( Test-Path $Target ) { # Get the full path of the target $t = Resolve-Path $Target if( Test-Path $Target -PathType Leaf ) { $s = cmd /c mklink $Path $t.Path } else { $s = cmd /c mklink /d $Path $t.Path } } else { Throw "Could not find target path" } # Create the link FormNewLinkResult $s "Symbolic" } #endregion #region Remove-HardLink function Remove-HardLink() { <# .SYNOPSIS Function used to remove a hard link. .DESCRIPTION Function used to remove a hard link. Command requires administrator privileges. .EXAMPLE Remove-HardLink -Path newdir This command will remove the hard link with name newdir. #> [CmdletBinding(DefaultParameterSetName = "Path")] Param( [Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)] [string] $Path ) # Check if the console has administrator privileges if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Check if path exists if( Test-Path $Path ) { # Get the full path $p = Resolve-Path $Path # Remove the link cmd /c del "$p" } else { Throw "Could not find link." } } #endregion #region Remove-JunctionLink function Remove-JunctionLink() { <# .SYNOPSIS Function used to remove a junction link. .DESCRIPTION Function used to remove a junction link. Command requires administrator privileges. .EXAMPLE Remove-HardLink -Path newdir This command will remove the junction link with name newdir. #> [CmdletBinding(DefaultParameterSetName = "Path")] Param( [Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)] [string] $Path ) # Check if the console has administrator privileges if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Check if path exists if( Test-Path $Path ) { # Get the full path $p = Resolve-Path $Path # Remove the link cmd /c rd "$p" } else { Throw "Could not find link." } } #endregion #region Remove-SymbolicLink function Remove-SymbolicLink() { <# .SYNOPSIS Function used to remove a symbolic link. .DESCRIPTION Function used to remove a symbolic link. Command requires administrator privileges. .EXAMPLE Remove-SymbolicLink -Path newdir This command will remove the symbolic link with name newdir. #> [CmdletBinding(DefaultParameterSetName = "Path")] Param( [Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)] [string] $Path ) # Check if console had administrator privileges. if( Test-IsAdmin ) { } else { Throw "This command should be run with administrator priviledges." } # Check if path exists if( Test-Path $Path ) { # Get the full path $p = Resolve-Path $Path $item = Get-Item $P if($item.LinkType -eq "SymbolicLink" -and $item.PSIsContainer -eq $true) { # Remove the directory link cmd /c rmdir /s /q "$p" } else { # Remove the file link cmd /c del "$p" } } else { Throw "Could not find link." } } #endregion #region Helper Functions function FormNewLinkResult() { Param( [Parameter(Position = 0, Mandatory = $true)] [string]$s, [Parameter(Position = 1, Mandatory = $true)] [ValidateSet('Junction', 'Symbolic', 'Hard')] [string]$type ) if($type -eq "Symbolic") { $expression = '^symbolic link created for (.*) <<===>> (.*)$' } if($type -eq "Junction") { $expression = '^Junction created for (.*) <<===>> (.*)$' } if($type -eq "Hard") { $expression = 'Hardlink created for (.*) <<===>> (.*)$' } $result = [System.Text.RegularExpressions.Regex]::Match($s, $expression) $obj = New-Object PSObject -Property @{ "Link" = $result.Groups[1].Value "Target" = $result.Groups[2].Value } $obj } #endregion #region Exports Export-ModuleMember -Function New-HardLink Export-ModuleMember -Function New-JunctionLink Export-ModuleMember -Function New-SymbolicLink Export-ModuleMember -Function Remove-HardLink Export-ModuleMember -Function Remove-JunctionLink Export-ModuleMember -Function Remove-SymbolicLink #endregion |