All power to the powershell – automate windows

Hi humans!

Do you have hundreds of thousands of files stored in even more drives and lost the overview already looong time ago? Right! #Me too...whatever. Time for spring cleaning! Let's figure out how to get a plan.

HINT: Find the finished script at the very bottom!

 

THE SETUP


  1. You need to install "Visual Studio Code" from here (it's 4 free)
  2. By installing it will ask you if you want to add it to THE PATH. That's okay. It simply means you can access this tool via the standard console of your computer. 

     Wiki: "PATH [...] specifying a set of directories where executable programs are located" 

  3. directly after creating a new file we save it as "PS1" in the root folder we want to search through:
  4. Install the PowerShell extension:
  5. Restart as Administrator
  6. Let the fun begin!

 

 

Let's start coding. Or..wait! We don't have a plan what we want to achieve. In programming 50% of your work is to have a good understanding where to go before we even do a step.

THE PLAN


 

  1. get the location of the script (later you put it in any folder you like to browse through)
  2. look for all the files (children) of the chosen directory
  3. get all files of a specific filetype (e.g. .mp3)
  4. create a .txt file
  5. list all files in a .txt file(name of the file & path were to find them)
  6. empty the list each time you run the script (otherwise it will just continue listing everything at the bottom)

Aaaand...whats that? Here is a quick guide to Visual Studio Code (= VSC). It's a really slim editor so let's make it quick.

THE EDITOR


 

Basically all you need is the editor window where you will write your code (where the cursor is). The Debug window will be very helpful because there we will see all the files, directories etc.

Okidoki..now the fun part 🙂

THE SCRIPT


 

  1. We store the path we want to search through in a local variable "TargetPath"
  2. "Test-Path" is a command that simply checks if the path (our "TargetPath") exists on the running system or not.
  3. If our path return true (means that it exists) it will print the path in the console.
  4. No magic - but let's check if it works!
  5. Open the Debug-Mode
  6. Open the Console and hit "run":
  7. Ok. Now we are at C:\. Before we continue if it also works with a different path:
  8. Hooray! Now create some test mp3 files (you can simply rename a .txt file to whatever.mp3) in the path. I'll stick to the Desktop here. Let''s collect some childs with:
    $childs = Get-ChildItem $TargetPath -Recurse -Filter *.mp3
    
    Write-Output "$($childs)"

    The local variable "$childs" =  [gets child items] in  [$our chosen path] and  [- in any subfolder] [- filtered to files that have an ".mp3" ending]
    Let's test:

  9. Alright! Now we create a new .txt-file where we will store all the data later.
    New-Item $TargetPath\MyMissingFiles_$((Get-Date).ToString('yyyy-MM-dd')).txt -ItemType file

    We create a [new item] in the [path of our choice]\[called "MyMissingFiles_currentDate"].txt as a [regular file].
    The date may seem a bit complex but once you got it you will never forget it 🙂

    $((Get-Date).ToString('yyyy-MM-dd-hh-mm'))

    $ = local variable
    (Get-Date) = the current date or a date that you need to specify will return as an object
    .ToString = we want to have the date-object as a string
    ('yyyy-MM-dd') = specify the format as the date will we dispayed
    You can find more infos and other date-formats here.

  10. YESSS! The console shows the result we expected:
  11. Great! We have an empty file...wonderful. Not feed it with some files. Those three files we want to see in out .txt file soon:
    You will habe this view if you put on a breakpoint and run the debug mode.
  12. I stored the .txt we created in a local variable "$Output" (so we don't need to re-write everything again) and saved all items in it:
    Well uhm yeah...thats a pipe |
    It catches the output of the command and pass it to another command. Means we grab the fullname of a child and hand it over to the "Add-Content" command. It's a lightweight writing for an 'and'. Grab it AND write it.
  13. All the files are well-listed in out .txt file we created:
  14. Time for improvement!
    To make it quicker to use the script it makes sense to store the [file format] we are searching for in a local variable at the top.


    As -Filter only allows one String we can filter for I replaced it with -Include. That can handle multiple *.extensions.

Oh boi! Here you can copy/pasta the script.

THE SCRIPT


$TargetPath="C:\Users\Name\Desktop" # aka any location you want
$FileTypes = Get-ChildItem $TargetPath -Recurse -Include *.txt, *.mp3
if (Test-Path $TargetPath)
{
  $Output=New-Item$TargetPath\MyMissingFiles_$((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss')).txt -ItemType file
  $Output#create .txt file
  "Searched folder: $TargetPath"|Add-Content$Output
  "Script directory: $(Get-Location)"|Add-Content$Output
  "---------------------------------"|Add-Content$Output
  $childs=$FileTypes#get specified items
  Write-Output"$($childs)"
  #add each item with it's name and path to .txt file:
  foreach($childin$childs)
  {
    $child.Fullname|Add-Content$Output
  }
}
else
{
  Write-Error"$TargetPath Pfad existiert nicht"
}