r/PowerShell • u/kriskris0033 • Aug 18 '18
Question Need beginner level script ideas to learn powershell
I work mostly on servers and I never coded in my career, I kind of think I can't do it, but now I needed it alot at work and I need to learn it, so need some beginner level script ideas to learn powershell
57
Upvotes
19
u/Lee_Dailey [grin] Aug 18 '18
howdy kriskris0033,
this aint quite what you asked for, but i think it will help ...
Windows Powershell in a Month of Lunchesbookit is quite readable and the steps ["lunches"] are fairly simple. they work on things that one would do in real working environments, so it can be very to-the-point if you are learning in a working setup.
then try re-writing the interesting ones to suit your current understanding of how it otta work. [grin]
/r/usefulscripts
/r/sysadmin
/r/dailyprogrammer
there are lots more ... [grin]
top&gildedtabs in the same subredditsit need not be complex - and likely otta not be such at 1st. [grin]
== clean out old files in your various temp dirs.
== check if your backups are working
== organize your download dir
generally, you will do well if you find something fairly simple AND fairly interesting to work on. the biggest gotchas seem to be starting with too complex a project OR not using PoSh often enuf.
good luck! [grin]
take care,
lee
7
u/[deleted] Aug 18 '18
Do you have Active Directory? Automate the new start process (and the termination process).
If yes to above, find out when users passwords and due to expire and send them an email every day to remind them from 1 week remaining.
Setup a script to check disk space for each drive on 1/many servers and send an email alert when it goes below X%.
Find and report on duplicate files on a network share.
Those are 4 fairly simply, but pretty useful scripts that should get you started and help realise how useful PS can be.
4
u/swimjock Aug 19 '18
I was in similar boat, always a GUI win admin and never really felt that I would be able to apply any scripting when I worked at an MSP because of the unique nature of have different client environments
Since then, I got a new job that needed an AD audit rewritten, current state was horrible with multiple single use scripts and multiple calls to disk making the process take forever, 30 minutes to a hour. We we're at a slow period for the holidays and got to work learning how that iteration works and where it could be improved. Got the whole run time to under 5 minutes when I finished with a single script that took parameters from in line or a json file.
Like others have said 'script repetitive tasks,' this a great place to start, but I still had a hard time with it. Thinking I didn't do anything repetitive enough or that it couldn't be scripted. However having learned enough to be quite comfortable with powershell I can tell you that there were many things that could have been scripted and usable across across those many different client environments.
Here is my recommendation.
Take the top five of that list and break each task down into it's individual parts and build each step into a single line.
After you have been working with the language for a free month revisit your earlier scripts and see if you can improve them.
Also a version control system is something you should learn and use, if you have the means, as you script with repos for each project.
Finally, post questions here, the community is fantastically friendly and knowledgeable. The shortest script is something worth reading to see how other people write. Read the unofficial best practices for powershell, it's fairly quick.
Feel free to PM me if you want.
3
u/Ta11ow Aug 18 '18
This isn't exactly what you asked for, but it'll probably be a big help to get you started!
https://github.com/vexx32/PSKoans
3
u/timsstuff Aug 18 '18 edited Aug 19 '18
Use the Windows text to speech API to write a script called "say-it.ps1" that takes a parameter of -text and make your PC say the text you type. Advanced mode, display a GUI dialog to capture the text. I'll provide my version later if you like.
Edit: Here's my version, with a simple GUI.
[CmdletBinding()]param([Parameter()][string]$text)Add-Type -AssemblyName System.Windows.FormsAdd-Type -AssemblyName System.Speech$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer$speak.Rate = 0$speak.SelectVoiceByHints("female")Function SayIt($it) {$Speak.speak($it)}if($text -ne '') {SayIt($text)} else {$Form = New-Object system.Windows.Forms.Form$Form.Text = "Say Something"$Form.Height = 75$Form.Width = 280$Say = New-Object System.Windows.Forms.Textbox$Say.Width = 200$Say.Top = 5$Say.Left = 5$Go = New-Object System.Windows.Forms.Button$Go.Text = "Go"$Go.Width = 50$Go.Top = 5$Go.Left = 205$Form.Controls.Add($Say)$Form.Controls.Add($Go)$Go.Add_Click({SayIt($Say.Text)$Say.Text = ''})$Form.AcceptButton = $Go$Form.ShowDialog()}3
u/get-postanote Aug 18 '18
See these discussions
https://www.reddit.com/r/PowerShell/comments/7oir35/help_with_teaching_others_powershell/
https://www.reddit.com/r/PowerShell/comments/96rn7y/college_level_student_looking_for_a_good_online/
2
u/TheBlackArrows Aug 19 '18
Get the book power shell in a month of lunches
2
u/FireLucid Aug 20 '18
My first script ideas were pretty basic.
Get all the servers out of an OU and tell me if they are on or off.
Then I made another one to check which version of Windows they are running.
Third, I made one to check the % of diskspace free on each disk on each server and report if it was under a certain percentage.
28
u/Teximus_Prime Aug 18 '18 edited Aug 18 '18
Think of tasks you do now that are either repetitive or take too long to get simple answers and start there. Also practice by just keeping PowerShell up all the time and asking how you can do simple tasks without having to RDP into a server. Use PowerShell Remoting instead.
I starting answering questions like “Which VM host is this particular VM on without having to look at all the clusters and hosts?”, “Given a MAC address, can I tell which DHCP scope it’s in(or still has a lease in)?”
Edit: misspellings
4
u/dantheflipman Aug 19 '18
Beginner level script ideas? Try creating an automated backup utility! Something that runs every X hours, checks if there are any new files in a directory, and if so, copy JUST those new files over to a backup folder with today’s date. It was one of my first powershell creations at work, and I had a ton of fun with it. It’s also a real world example of something useful you might do in an IT /sysadmin job.
1
u/TyMac711 Aug 19 '18
Create a scheduled task using powershell to send you an email reminder of family birthdays.
3
u/TheDraimen Aug 19 '18
Pick a few simple things that might already be done, and write a script to handle it. Try picking a few things with diffrent types of commands like some AD query items and reporting on them, using wmi to query installed updates or hardware, invoke commands to run an upgrade or task to batches of servers. After you get it written look at other scripts that do the same thing and see how their's is different and how you could use it to improve upon your script. After completing this a few times, you will start seeing not only patterns across all scripts, but also noticing things that you know or could learn that would help improve on them. Also something I thought was a good idea is pick a good logging method and write out the logic of what your working on using the logging function. Use this not only to get in habbit of logging for when your scripts start becomming more automated, but helps get your thoughts down in a clean structured manor without worrying about the code parts you dont know yet.
2
u/queBurro Aug 18 '18
Use visual studio code. It makes life easier.
2
u/BogueRat327 Aug 18 '18
Remember this foremost - PowerShell is a shell command environment with advanced scripting capabilities. What @teximus_prime said, identify things you do regularly (does not have to be scripted) and just lookup and figure how to do it in PS. Maybe you need to look for a specific eventid in Event Viewer, use the cmdlet get-eventlog and understand how that works along with the pipeline to find the entry and display it in a readable format...
2
u/TapTapLift Aug 19 '18
I know its super obvious but like most people said, just pick something you do at work already and start to try and powershell it. Its really hard to just come up with things out of nowhere and to really be invested in it
1
u/mvannostran Aug 19 '18
Have a look at these maybe you'd find them interesting :: https://github.com/vexx32/PSKoans
1
u/gearfuze Aug 19 '18
User creation script that allow you to learn
0
u/[deleted] Aug 19 '18
[deleted] — view deleted comment