03 / FILES AND FORMATS · LESSON 09

Find your way around

Create a directory and verify that it exists.

10 min · learn at your pace

A workspace you can explore

Your lab starts in /home/user. This is a virtual directory in the browser. Get-Location shows your current location, and Get-ChildItem lists its contents.

Get-Location
Get-ChildItem

An absolute path starts at the root, such as /home/user/reports. A relative path starts from your current directory. Here, ./reports points to the same place.

Create and check

New-Item -ItemType Directory -Path './reports'
Test-Path -Path './reports' -PathType Container

New-Item returns the created item. Pipe it to Out-Null when you do not need that output. Test-Path returns a Boolean: $true or $false.

Join-Path combines directory and child names without hand-building path separators. Set-Location changes the current directory.

The lab uses Unix-style paths. A Windows machine may use paths such as C:\Users\Ada; native drives and system folders are not present in this browser workspace.

Try an example.ps1
Get-Location
Get-ChildItem
YOUR TURN

Put it into practice.

Create a reports directory under /home/user, then output whether it exists as a directory.

See an explained solution
One way to solve it.ps1
New-Item -ItemType Directory -Path './reports' -Force | Out-Null
Test-Path './reports' -PathType Container

-Force allows the script to run again when the directory already exists. Suppressing New-Item's output leaves the Boolean check as the final result.