Q:

Powershell script not creating a folder using mkdir function

I'm looking to create daily folders using a Powershell script, and while the first 2 folders are being created no problem, the third folder on my list isn't being created. Below is my code, and it's the Raw Data folder that's not being created for some reason - any suggestions as to why this may be happening?
$months = Get-Date -UFormat %b
$monthl = Get-Date -UFormat %B
$year = Get-Date -UFormat %Y
$timestamp = Get-Date -UFormat "%d%m%Y"
$folderstamp = Get-Date -UFormat "%d-%m-%Y"

mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp"
mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp\Final Output"
mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp\Raw Data"

If I write out that line of code on Powershell itself, it returns a LastWriteTime date of 01/01/1601?! See screenshot link below. The mode seems to show all possible modes available?
powershell screenshot

A:

From your screenshot, I can see the Raw Data folder indeed exists. Under Mode you can see its attributes:

d - Directory
a - Archive
r - Read-only
h - Hidden
s - System
l - Reparse point, symlink, etc.

Maybe you should investigate that folder (or link) some more to find out why it is there and if it is a symlink, where it points to.
Anyway, here's your code in more PowerShell style:
$now         = Get-Date
$months      = $now.ToString("MMM")
$monthl      = $now.ToString("MMMM")
$year        = $now.Year
$timestamp   = $now.ToString("ddMMyyyy")
$folderstamp = $now.ToString("dd-MM-yyyy")

$folderName = "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp"
try {
    New-Item -ItemType Directory -Path $folderName -ErrorAction Stop | Out-Null
    New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Final Output') -ErrorAction Stop | Out-Null
    New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Raw Data') -ErrorAction Stop | Out-Null
}
catch {
    Write-Error $_.Exception.Message
}

Hope that helps

