We may have a lot of files on the desktop some times 20,40 and go on. Too many word doc, excel, Jpeg and screenshot, text files. we would have faced a situation where we will be searing a file on the 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
- $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
- }