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

Rearrange Files In Desktop With Their Extensions

  1. $DesktopPath = [Environment]::GetFolderPath(“Desktop”)
  2. $Dir = get-childitem $DesktopPath -file
  3. $Dir | foreach-Object{
  4. $extname= $_.extension
  5. $Dname=$_.DirectoryName
  6. $Fname=$_.FullName
  7. $extFolder=$Dname+”\”+$extname
  8. CreateFolder ($extFolder)
  9. MoveFile ($Fname,$extFolder)
  10. }
  11. Function CreateFolder ($extFolder){
  12. If(!(test-path $extFolder))
  13. {
  14. New-Item -ItemType Directory -Force -Path $extFolder
  15. }
  16. }
  17. Function MoveFile ($source,$destination) {
  18. Move-item -path $Fname -Destination $extFolder
  19. }