Private/Set-Path.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Set-Path {
  <#
  .SYNOPSIS
    Change the current directory.
  .DESCRIPTION
    This is meant to enable consistency with new PowerShell windows. With the
    same directory opening by default when a profile is loaded, you can ensure
    you always start from the same blank slate.
  .PARAMETER Path
    Change the current directory to this value.
 
    Create this directory if it does not already exist.
  .PARAMETER Force
    Controls whether or not the user is prompted to change directory.
  .EXAMPLE
    Set-Path 'C:\Projects'
  #>

  [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
  Param (
    [ValidateScript( {
        if (-not (Test-Path $_) ) {
          Write-Verbose "Creating default location $_"
          New-Item -ItemType Directory -Force -Path $_
        }
        return $true
      })]
    [System.IO.FileInfo]$Path,
    [switch]$Force
  )
  if ($Force -or $PSCmdlet.ShouldProcess($Path, 'change current directory.')) {
    Write-Verbose "Setting path to $Path."
    Set-Location $Path
  }
}