FavouritePaths.psm1

#######################
# Code by COPPERAZIDE #
#######################

$FavouritePaths = Split-Path $script:MyInvocation.MyCommand.Path
$FavouritePaths += "\FavouritePaths.xml"

$global:MyPaths=@{}

function save {
    $MyPaths | Export-Clixml $FavouritePaths
}

function addNocheck($name, $path) {
    $MyPaths.Add($name, $path)
    Set-Variable -Name $name -Value $path -Scope Global
}

function addNosave($name, $path) {
    if($MyPaths.ContainsKey($name)) {
        Write-Verbose "Pathkey ""$name"" already exists." -Verbose
    } elseif (!(Test-Path $path)) {
        Write-Verbose "The specified path ""$path"" does not exist." -Verbose
    } else {
        addNocheck $name $path
    }
}

function renNosave($name, $newName) {
    if(!($MyPaths.ContainsKey($name))) {
        Write-Verbose "Pathkey ""$name"" does not exist." -Verbose
    } elseif($MyPaths.ContainsKey($newName)) {
        Write-Verbose "Pathkey ""$newName"" already exists." -Verbose
    } else {
        $value = $MyPaths.$name
        $MyPaths.Remove($name)
        addNocheck $newName $value
    }
}

function deleteNosave($name) {
    if(!($MyPaths.ContainsKey($name))) {
        Write-Verbose "Pathkey ""$name"" cannot be removed because it does not exist." -Verbose
    } else {
        $MyPaths.Remove($name)
        Remove-Variable -Name $name -Scope Global
    }
}



function Add-Path{

    [CmdletBinding()]
    Param( 
        

        [Parameter(Mandatory=$true)]
        [String]$name,

        [Parameter(Mandatory=$true)]
        [String]$path
    )

    Begin {
        addNosave $name $path
        save
    }

    <#
 
    .DESCRIPTION
 
    Adds a variable called "name" (first parameter) that stores a path (second parameter).
    Only existing paths can be stored.
    The path stored in the variable name can be called with $name.
    To open this path in the explorer, type "ii $name".
    Any exisiting variable will be overwritten, unless it's a variable that was created with this command.
    All variables are stored in a table, which can be called with $MyPaths.
    The variable is saved in an XML file and restored when the module is re-imported.
 
    .PARAMETER name
    Name of the variable
 
    .PARAMETER path
    Path that is stored in the variable
 
    .EXAMPLE
 
    PS> Add-Path documents C:\Users\Michael\Documents
    PS> $documents
    C:\Users\Michael\Documents
    #>

}

function Rename-Path{
    [CmdletBinding()]
    Param( 
        #Name of the Path Variable
        [Parameter(Mandatory=$true)]
        [String]$name,

        #New name of the Path Variable
        [Parameter(Mandatory=$true)]
        [String]$newName
    )

    Begin {
        renNosave $name $newName
        save
    }

    <#
 
    .DESCRIPTION
 
    Renames a variable that was created with the Add-Path Command.
    The first parameter specifies name of the variable that should be renamed, the second parameter the new name.
    The variable will also be renamed in the XML file and in the MyPaths table.
 
    .PARAMETER name
    Name of the variable
 
    .PARAMETER newName
    New name of the variable
 
    .EXAMPLE
 
    PS> Add-Path documents C:\Users\Michael\Documents
    PS> Rename-Path documents newDocuments
    PS> $newDocuments
    C:\Users\Michael\Documents
    #>

}

function Remove-Path{
    [CmdletBinding()]
    Param( 
        #Name of the Path Variable
        [Parameter(Mandatory=$true)]
        [String]$name
    )

    Begin {
        deleteNosave $name $path
        save
    }     
    
    <#
 
    .DESCRIPTION
 
    Removes a path variable that was created with the Add-Path Command.
    The parameter specifies the name of the variable.
    The variable will also be removed from the XML file and from the MyPaths table.
 
    .PARAMETER name
    Name of the variable
 
    .EXAMPLE
 
    PS> Add-Path documents C:\Users\Michael\Documents
    PS> Remove-Path documents
    PS> $documents -eq null
    True
    #>

}

function getMyPaths {
    [CmdletBinding()] Param()
    Begin {
        if (Test-Path $FavouritePaths) {
            Write-Verbose "Importing Common Paths."
            $global:MyPaths = Import-Clixml $FavouritePaths

            foreach($key in $MyPaths.GetEnumerator()) { 
                $name = $key.name
                $path = $key.Value
                Set-Variable -Name $name -Value $path -Scope Global
                if (!(Test-Path $path)) {
                    Write-Warning "Path in variable ""$name"" does not exist ($path)."
                }
            }
        } else {
            Write-Verbose "Add a path variable with the command ""Add-Path"" or ""addP"" to save a given path. Type ""Get-Help Add-Path"" for further information." -Verbose
        }
    }
}

Set-Alias -Name myP -Value MyPaths -Scope Global
Set-Alias -Name addP -Value Add-Path -Scope Global
Set-Alias -Name delP -Value Remove-Path -Scope Global
Set-Alias -Name renP -Value Rename-Path -Scope Global

getMyPaths

Export-ModuleMember -Function Add-Path, Remove-Path, Rename-Path -Variable MyPaths