Directories in PowerShell

Check whether a directory exists

Check whether the current directory contains a subdirectory swagger-ui

${SwaggerPath} = Join-Path ${pwd} "swagger-ui"; If (Test-Path -path ${SwaggerPath}) { Write-Host "swagger-ui dir exists" -f Green } Else { Write-Host "swagger-ui dir does not exist" -f Yellow }

Check and if there is no such directory - download the archive

$SWAGGER_URL = "https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.11.10.zip"; ${SwaggerPath} = Join-Path ${pwd} "swagger-ui"; If (Test-Path -path ${SwaggerPath}) { Write-Host "swagger-ui dir exists" -f Green } Else { Write-Host "swagger-ui dir does not exist - starting download" -f Yellow Invoke-WebRequest $SWAGGER_URL -OutFile swagger-ui.zip }

Create a directory

To create a new directory, use the command New-Item with ItemType directory

New-Item -Path "c:\" -Name "logfiles" -ItemType "directory"

Check that the directory exists. If it does not exist - create it.

If (Test-Path -path "test\mod\etc\") { Write-Host "test\mod\etc\ dir exists" -f Green } Else { Write-Host "test\mod\etc\ dir does not exist - creating" -f Yellow New-Item -Path "test\mod\" -Name "etc" -ItemType "directory" }

Unpack an archive

Unpack an archive into the current directory

Expand-Archive -Path swagger-ui.zip -DestinationPath .

Rename a directory

Rename-Item -Path swagger-ui-5.11.10 -NewName swagger-ui

Join-Path

You can join paths as follows

$TMP_PATH = "C:/tmp_python" $TMP_PYTHON_EXE = Join-Path -Path $TMP_PATH -ChildPath "python.exe"

Article author: Andrei Olegovich