Rearrange files in Desktop with their extensions

ReginR
Apr 2, 2021

We may have lot of files in desktop some times 20,40 and go on. Too many word doc, excel, Jpeg and screenshot, text files. we would have face a situation where we will be searing a file in desktop one by one. So thought of having a folder for each extension. This script will create a folder with extension name and move those files with respective folders

Powershell code

$DesktopPath = [Environment]::GetFolderPath(“Desktop”)
$Dir = get-childitem $DesktopPath -file
$Dir | foreach-Object{
$extname= $_.extension
$Dname=$_.DirectoryName
$Fname=$_.FullName
$extFolder=$Dname+”\”+$extname
CreateFolder ($extFolder)
MoveFile ($Fname,$extFolder)
}
Function CreateFolder ($extFolder){
If(!(test-path $extFolder))
{
New-Item -ItemType Directory -Force -Path $extFolder
}
}
Function MoveFile ($source,$destination) {
Move-item -path $Fname -Destination $extFolder
}

--

--