Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
powershell script to make directories for each file in a directory and move those files into them.
# This script will make directories for all files in a specified directory and move them into it.
# You must provide the input directory as the first argument and can optionally specify an output
# directory as the second argument. If no output directory is specified the input directory will
# be used.
$inDir = $args[0]
$outDir = $args[1]
$isInDir = Test-Path -LiteralPath $inDir -PathType Container
if ($outDir -ne $null) {
$isOutDir = Test-Path -LiteralPath $outDir -PathType Container
if ($isInDir -eq $false) {
throw "Error: $inDir : is not a directory"
if ($isOutDir -eq $false) {
throw "Error: $outDir : is not a directory"
$files = Get-ChildItem -LiteralPath $inDir -File
foreach ($f in $files) {
$fExt = $f.Extension
if ($outDir -ne $null) {
$fDir = -join($outDir,"\",$f.BaseName)
} else {
$fDir = $f.FullName.Replace($fExt,"")
New-Item -Path $fDir -ItemType Directory | Out-Null
Move-Item -LiteralPath $f.FullName -Destination $fDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment