Powershell "Move-Item" does not create a directory if it does not exist

I have a Powershell script that does a lot of things, and one of them moves files:

$from = $path + '\' + $_.substring(8) $to = $quarantaine + '\' + $_.substring(8) Move-Item $from $to 

But the directory structure no longer exists in the $to path. Therefore, I would like Powershell to create it with this commando. I tried Move-Item -Force $from $to , but that didn't help.

What can I do to make sure Powershell creates the necessary directories to make everything work?
Hope I clarify if not, please ask!

+8
powershell
source share
2 answers

You can create it yourself:

 $from = Join-Path $path $_.substring(8) $to = Join-Path $quarantaine $_.substring(8) if(!(Test-Path $to)) { New-Item -Path $to -ItemType Directory -PathType Container -Force | Out-Null } Move-Item $from $to 
+7
source share

You can use the system.io.directory.NET class to check the destination directory and create if it does not exist. Here is an example of using your variables: -

 if (!([system.io.directory]::Exists($quarantine))){ [system.io.directory]::CreateDirectory($quarantine) } Copy-File $from $to 
+3
source share

All Articles