The promised v2 module…


As I promised in my last post, I spent some time working on the function for setting an alias for a file based on a wildcarded folder path.

I’ve added support for a “-Force” switch so the default functionality doesn’t overwrite an existing alias, as well as replacing some .Net style calls with PowerShell equivalents. And the whole thing was wrapped in a nice v2 module format with help. I’ve also removed the -Quiet switch and moved to using the built-in Write-Verbose commandlet, so that you can use the -Verbose switch instead.

Enjoy!

function Set-FileAlias()
{
    <#
    .SYNOPSIS
        Sets an alias for a file using a wildcard path.
    .DESCRIPTION
        An alias will be set for the first occurence of the file name provided that is found on the path. If a path is not provided the search will start from the current folder.
        Unlike the Set-Alias command, this command will not overwrite an existing alias unless you add the '-Force' switch.
    .EXAMPLE
        Set-FileAlias np notepad.exe
        The above command will look for the file "notepad.exe" starting from the current path. If it is found, it will be assigned the alias 'np'.
    .EXAMPLE
        Set-FileAlias np notepad.exe "C:\windows\"</span>
        The above command will look for the file "notepad.exe" starting from the path 'C:\Windows\'. If it is found, it will be assigned the alias 'np'.
    .PARAMETER alias
        The name of the alias to be added.
    .PARAMETER fileName
        The fileName that the alias is to reference.
    .PARAMETER pathPattern
        A folder to start searching from. This may contain both the '*' and '?' wildcards. If not provided, it will
        default to the current folder.
    #>

    param(
        [Parameter(Position=0, Mandatory=$true)]
        [string]$alias,
        [Parameter(Position=1, Mandatory=$true)]
        [string]$fileName,
        [Parameter(Position=2, Mandatory=$false)]
        $pathPattern = "*",
        [Parameter(Mandatory=$false)]
        [switch]$Force
    )

    # Check the input values
    if (!(Test-Path $pathPattern)) {
        Write-Warning "Could not find any paths to match $pathPattern"
        break
    }

    if (!$Force -and ((Get-Alias $alias -ErrorAction SilentlyContinue) -ne $null)) {
        Write-Warning "The alias '$alias' is already defined. Use the -Force switch to override."
        break
    }

    # If the user specified a wildcard, turn the pathPattern into an array of matching items
    # We don't always want to do this, because specifying a non-wildcard directory gives false positives
    if (($pathPattern -match "[*]") -or ($pathPattern -match "[?]")) {
        $pathPattern = Get-ChildItem $pathPattern -ErrorAction SilentlyContinue
    }

    $files = @($pathPattern | ForEach-Object { Get-ChildItem $_ -Recurse -Filter $fileName -ErrorAction SilentlyContinue })

    if ($files.Count -eq 0) {
        Write-Warning "Could not find $fileName in searched paths:"
        $pathPattern | ForEach-Object { Write-Warning " $_" }
        break
    }

    Set-Alias $alias $files[0].FullName -scope Global

    Write-Output "Added alias '$alias' for '$($files[0].FullName)'"
    if ($files.count -gt 1) {
        Write-Verbose "There were $($files.count) matches:"
        $files | ForEach-Object { Write-Verbose " $($_.FullName)" }
    }
}

Export-ModuleMember Set-FileAlias
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: