r/PowerShell • • Oct 25 '21

Game for learing Powershell?

Hey there

Is there a game I can learn about Powershell? Or any free courses? like https://flexboxfroggy.com/ . I want up my skills.

NazoN

134 Upvotes

30 comments sorted by

View all comments

2

u/Phx86 Oct 25 '21 edited Oct 26 '21

Do you have access to an AD environment and rights to do things?

I find that structuring a framework is nice to have on hand and modifying it to do what I want. For example if I need to run a set of commands on a bunch of machines I use this.

 #Comment out the section based on how you are providing inputs

#Get servers from an OU
#$OU = 'OU="Some",OU="Other",DC=contoso,DC=local'
#$Servers = Get-ADComputer -filter * -SearchBase $OU | Select-object -ExpandProperty Name

#List of servers 
#$servers = @("Server1", "Server2", "Server3")

#Servers from a .csv
#$Servers = Import-Csv .\server.csv

Foreach ($Server in $Servers){
    Invoke-command -ComputerName $Server -scriptblock {
    Param($server, $servers)
        write-host $Server
        gpupdate /force
        Write-Host Policies updated
        w32tm /config /syncfromflags:DOMHIER /update
        net stop w32time
        net start w32time
        w32tm /resync
        } -argumentlist $server, $servers
    }

edit: not sure why I can't paste as a code block, but this is close enough that it is readable.

In this example I was undoing a GPO that set time on the servers and moving everything back to using domain hierarchy. I removed the policy on an OU, then ran the OU based input to grab all the servers in that OU and run a block of commands on them.

Replace everything inside the script block from the write-host to w32tm /resync and you can do whatever instead. Install an app, edit the registry, whatever... Obvious this would apply to workstations too if you wanted, just how I had the variables named for my use.

3

u/PMental Oct 25 '21

edit: not sure why I can't paste as a code block,

One blank line first, then all your code, each line indented 4 spaces, and you get a code block:

# Like
# this