Master PowerShell Tricks Volume 2
Master PowerShell Tricks Volume 2
About the Book
There are many cool things about Windows PowerShell. For me three of the most awesome things are the following:
1. If you don’t like the way Windows PowerShell does things, you can change it.
2. If Windows PowerShell can’t do something you need, you can add it.
3. The Windows PowerShell community is super dedicated, and will help you do both one and two.
Now, to be honest at times, it is necessary to write hundreds and hundreds of lines of arcane code, to dive into the deepest and darkest mysteries of programing, and even to learn about things like API’s, Constructors, Events, threading, ACL’s, DACL’s, CACL’s and maybe even Tetradactylies. Then again, most of the time it is not. In fact, it has been years and years since I wrote a hundreds and hundreds of lines of arcane code.
It is almost as if the Windows PowerShell team deliberately tried to make Windows PowerShell easy to use and easy to learn. Hmm … I wonder if that approach would ever catch on? Anyway, there used to be an old saw: “Ease of use is directly opposed to program capability.” Or, in other words, if it is easy to use, it probably is not all that powerful. Well, PowerShell changes that … dramatically.
And yet, Windows PowerShell is also deceptively easy to use. One can go from Get-Service and Get-Process or even Get-Date to some pretty complicated stuff in like one line of code.
This is why item number three is so important. The authors of this book: Dave, Sean, Will, Mick, Thomas and Allan are all Windows PowerShell experts, and have even been recognized by Microsoft as community leaders. So, this means not only do they know their stuff, but they are also great at sharing that knowledge with the community. Sean Kearney is even an Honorary Scripting Guy – a very elite group indeed!
One of the great way that MVP’s share their knowledge and experience is via MVP Days a traveling road show that was started by Dave and Cristal Kawula. This is a very well run event, and I have had the opportunity to speak at two of the events … it is cool, and it is fun.
So grab this book, get it autographed, and learn how to master some awesome PowerShell tricks. It is cool.
Ed Wilson
Microsoft Scripting Guy
@ScriptingGuys
Bundles that include this book
Table of Contents
Foreword Ed Wilson “The Scripting Guy” iii
Acknowledgements v
From Dave v
About the Authors vi
Dave Kawula - MVP vi
Sean Kearney - MVP vii
Thomas Rayner - MVP vii
Allan Rafuse – MVP viii
Will Anderson – MVP ix
Mick Pletcher – MVP x
Ed Wilson – The Scripting Guy xi
Technical Editors xii
Cristal Kawula – MVP xii
Emile Cabot - MVP xii
Kai Poynting xiii
Contents xiv
Introduction 1
North American MVPDays Community Roadshow 1
Structure of the Book 2
Sample Files 3
Additional Resources 3
Chapter 1 5
PowerShell Regex to Get Value Between Quotation Marks 5
Chapter 2 7
How to Send an Email Whenever a File Gets Changed 7
Chapter 3 8
Using PowerShell to Add Groups to Accept messages only Distribution list members 8
The Code 9
Chapter 4 10
Installing and Finding Modules 10
Why use Install-Modules 10
Module Dependencies 10
Repositories 11
Managing Repositories 11
Finding Modules in the Repositories 11
Installing a Module 13
Proxy Configuration 14
Summary 14
Chapter 5 15
Use PowerShell to Validate Parameters for you 15
Common PowerShell Functions – NO Validation 16
PowerShell Features for Parameters 16
Problem 1 16
Problem 2 17
Problem 3 17
Solution 18
Other Parameter Validation Features 18
Chapter 6 19
PowerShell – My Way to Prepare a SQL Server VM 19
Template File 20
A look at library-PrepareSQLServer.ps1 21
Running the script 23
Chapter 7 25
Building Gold VHDx images with PowerShell 25
Define the Variables 26
Build $unnatendSource 26
Get-UnattendChunk 27
New-Unattendfile 28
New-BaseImage 29
Chapter 8 32
Using Try Catch to help with Decision Making in a Script 32
Chapter 9 36
Invoking your SCCM Client Remotely with PowerShell 36
Chapter 10 38
Use PoweShell to Parse RSS Feeds 38
Chapter 11 44
How to alter the public IP Address of an Azure Virtual Machine using PowerShell 44
The Challenge 44
Data formatting or using an existing example 44
Make the change 46
Chapter 12 48
Get the Public IP of an Azure VM with PowerShell 48
Chapter 13 49
SCCM Local Administrator Reporting with PowerShell 49
LocalAdmins.ps1 53
Chapter 14 58
Configuring Power Settings using PowerShell 58
Set-PowerScheme.ps1 58
Chapter 15 73
Automated SCCM Endpoint Full System Scan upon Infection with Email Notification 73
ApplicationVirusDetectionMethodEmail.ps1 75
AntiVirusScanEmail.ps1 76
Chapter 16 79
Laptop Mandatory Reboot Management with SCCM and PowerShell 79
MandatoryReboot.ps1 80
MandatoryRebootCustomDetection.ps1 82
Chapter 17 84
Set Windows Features with Verification 84
WindowsFeatures.ps1 85
Chapter 18 90
Easily Restore a Deleted Active Directory User with PowerShell 90
Chapter 19 91
Getting Large Exchange Mailbox Folders with PowerShell 91
Chapter 20 95
Getting your Organizations Largest Exchange Mailboxes with PowerShell 95
Chapter 21 99
Just Enough Administration (JEA) First Look 99
So how do you get started? 99
Chapter 22 105
Does a String Start or End in a Certain Character? 105
Chapter 23 107
Using PowerShell to List All the Fonts in a Word Document 107
Chapter 24 109
Find PowerShell Scripts in the GUI 109
Chapter 25 116
How to use PowerShell as an Administrative Console 116
Chapter 26 118
How to Erase Files based on Date using PowerShell 118
Chapter 27 121
Join us at MVPDays and meet great MVP’s like this in person 121
Live Presentations 121
Video Training 121
Live Instructor-led Classes 121
Consulting Services 122
Twitter 123
Other books by these authors
Authors have earned$10,095,386writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
The book is almost daily updated. These incremental updates ease my interaction with the proofreaders.
A Guide to Artificial Intelligence in Healthcare
Dr. Bertalan MeskoCan we stay human in the age of A.I.? To go even further, can we grow in humanity, can we shape a more humane, more equitable and sustainable healthcare? This e-book aims to prepare healthcare and medical professionals for the era of human-machine collaboration. Read our guide to understanding, anticipating and controlling artificial intelligence.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Introducing EventStorming
Alberto BrandoliniThe deepest tutorial and explanation about EventStorming, straight from the inventor.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Everyday Rails - RSpecによるRailsテスト入門
Junichi Ito (伊藤淳一), AKIMOTO Toshiharu, 魚振江, and Aaron SumnerRSpecを使ってRailsアプリケーションに信頼性の高いテストを書く実践的なアドバイスを提供します。詳細で丁寧な説明は本書のオリジナルコンテンツです。また、説明には実際に動かせるサンプルアプリケーションも使用します。本書は2017年版にアップデートされ、RSpec 3.6やRails 5.1といった新しい環境に対応しています!さあ、自信をもってテストできるようになりましょう!
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
The Hundred-Page Machine Learning Book
Andriy BurkovEverything you really need to know in Machine Learning in a hundred pages.
Continuous Delivery Pipelines
Dave FarleyThis practical handbook provides a step-by-step guide for you to get the best continuous delivery pipeline for your software.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #3
PowerShell
3 Books
Buy every PowerShell book from Adam Bertram at a 20% discount! - #4
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #5
Software Architecture and Beautiful APIs
2 Books
There is no better way to learn how to design good APIs than to look at many existing examples, complementing the Software Architecture theory on API design. - #6
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #7
Learn Git, Bash, and Terraform the Hard Way
3 Books
Learn Git, Bash and Terraform using the Hard Way method.These technologies are essential tools in the DevOps armoury. These books walk you through their features and subtleties in a simple, gradual way that reinforces learning rather than baffling you with theory. - #8
9 Books-Bundle: Shut Up and Code!
9 Books
"Shut up and code." Laughter in the audience. The hacker had just plugged in his notebook and started sharing his screen to present his super-smart Python script. "Shut up and code" The letters written in a white literal coding font on black background was the hackers' home screen background mantra. At the time, I was a first-year computer... - #9
Mastering Containers
2 Books
Docker and Kubernetes are taking the world by storm! These books will get you up-to-speed fast! Docker Deep Dive is over 400 pages long, and covers all objectives on the Docker Certified Associate exam.The Kubernetes Book includes everything you need to get up and running with Kubernetes! - #10
CCDE Practical Studies (All labs)
3 Books
CCDE lab