Ensure-Location.ps1 716 B

1234567891011121314151617181920212223242526272829
  1. function Ensure-Location {
  2. <#
  3. .SYNOPSIS
  4. Ensures current location to be set to specified directory.
  5. .DESCRIPTION
  6. If specified directory exists, switch to it. Otherwise create it,
  7. then switch.
  8. .EXAMPLE
  9. Ensure-Location "My-Directory"
  10. Ensure-Location -Path "Path-To-My-Directory"
  11. #>
  12. param(
  13. [Parameter(Mandatory)]
  14. [string] $Path
  15. )
  16. if ( ! ( Test-Path $Path ) ) {
  17. $_Params = @{
  18. ItemType = "Directory"
  19. Path = ${Path}
  20. ErrorAction = "SilentlyContinue"
  21. }
  22. New-Item @_Params | Set-Location
  23. } else {
  24. Set-Location -Path ${Path}
  25. }
  26. }