Places to learn Powershell?

Soldato
Joined
23 Nov 2007
Posts
4,956
Location
Lancashire, UK
Just wondering what the recommendations were for online places to learn powershell? I have a specific task I'm trying to achieve around iterating through all subfolders (only one level), renaming each file with a certain extension to its current name plus subfolder name, then moving the resultant file into the root directory. Up for teaching myself but there seems to be a lot of crap online so was wondering if there were any known good places to learn.

Cheers.
 
Last edited:
Soldato
Joined
25 Mar 2004
Posts
15,796
Location
Fareham
This should do what you want just run in a test method first, i.e. against a folder you've added with some files for testing.

This only goes 1 level deep.

Remove -WhatIf on the Move-Item command if it looks OK etc, otherwise it will just output what it would do. You may need to swap -WhatIf for -Confirm:$False

Code:
#Root Folder
$RootFolderPath = "C:\RootFolder\"
$RootFolderDirs = gci $RootFolderPath -Directory

#Loop SubFolders of RootFolder
foreach ($SubFolder in $RootFolderDirs)
{
    Write-Host $SubFolder.FullName
  
    #Get Files in current RootFolder
    $SubFolderFiles = gci $SubFolder.FullName -File
  
    #Loop Files
    foreach ($File in $SubFolderFiles)
    {
        #Move File. Format is RootFolderPath\SubFolderName_FileName.ext
        $NewPath = $RootFolderPath + "$($SubFolder.Name)_$($File.Name)"
        Move-Item -Path $File.FullName -Destination $NewPath -WhatIf
    }
}
 
Last edited:
Associate
Joined
31 Dec 2010
Posts
2,455
Location
Sussex
Not very helpful, but part of the reason Powershell is so confusing IMO is that it tries to do and be too many things:
Run DOS batch files? No problem.
Run Powershell native stuff? No problem.
Tab into the full .NET Framework? No problem.
etc.
Then go some things its own way like -eq, -gt, -lt, and similar things when most programmings languages whether C inspired or from a Basic/Pascal background get way with = (or == and even === in the case of some C syntax inspired ones) and > and < etc.
And the strings where ' and " mean different things.
But generally you should do this kind of thing using Powershell native cmdlets, so start with Get-ChildItem and work from there.

And it goes without saying that while move and rename aren't as destructive as delete, you should still test this all out using some test folder!

EDIT: never mind the Get-ChildItem, @HungryHippos code is far more readable!
 
Last edited:
Underboss
Joined
23 Oct 2013
Posts
11,355
Location
Guildford
Powershell in a month of lunches, I didn’t complete it but it gave me the grounding so needed to use it to an okay level.

Oh and “-whatif” is your friend
 
Last edited:
Soldato
Joined
25 Mar 2004
Posts
15,796
Location
Fareham
EDIT: never mind the Get-ChildItem, @HungryHippos code is far more readable!

gci is shorthand for Get-ChildItem.

You can use shorthands in places, some are more useful/memorable than others.

For example: $Variable | ? {$_.Property -eq "Thing"}

? replaces Where-Object in this case :)

I use these a fair bit as well:

$Variable | fl
$Variable | ft -a
$Variable | Out-Gridview

fl = format list
ft -a = autoformat table
Out-Gridview = send the output to a gridview object so I can see the data bit better without Export-Csv
 
Last edited:
Back
Top Bottom