I'll be using this post to provide tips and tricks I've learned with PowerShell. I keep most of my stuff at work, so perhaps tomorrow I'll bring some content to this post after I scrub my scripts of anything specific to our organization. I'll start with a few simple ones. Yes, these can be done with other methods, such as .bat files, or python, what have you, but I use PowerShell, and it's what I know. I'll be writing descriptions below, scripts to follow. Feel free to request scripts! I enjoy doing this, and it's good for job security. I'll likely even be updating scripts on here as well, as I learn of better ways to run them. For example, I'm building the driver update script as I write this post, but the one I wrote at work is much nicer, so I'll be replacing it with bits from that one.
A couple of quick things I want to mention.
Functions are basically meant to be scripts you can cache in your PowerShell profile, and allow passing variables in-line. I typically work with functions, but sometimes it doesn't quite fit the plan. Really, it depends on your tastes.
When you see $, that means it's defining a string as a variable.
When you see # at the beginning of a line, that code is disabled, or it is meant as a way of putting notes in the script. Nothing that follows a # in-line will run in a PowerShell script.
Function Template
For those interested in creating there own PowerShell functions, this is a template I use.
Installing Windows Updates
Some of us don't like automatic updates. Guess what? You always had the power, with PowerShell, to control it. We simply disable the update services, and add some scheduled tasks to keep that service disabled, and only download the updates we want, and use a simple script to install them manually, or we could add the script as a scheduled task to automatically install anything we download to a specific folder, we can even add checks to see if it's been installed, and then delete source files to save space.
CODE:
DESCRIPTION
Forcing Drivers
Sometimes a device is not officially supported by Windows 10, and if you just download and try to run the .exe, Windows tells you to get rekkd. We had this happen a lot with old printers, so I learned how to use the PnP Utility. I needed to do this for my old Alienware 17 R2 (2015), as the drivers from dell were causing USB glitches. I downloaded the H87 chipset drivers directly from Intel, and had to force them with this method, and the issues went away!
## This will use the PnP Utility to attempt to force drivers on a system.
Function Update-Drivers {
$SourceFiles = Read-Host "Where are the drivers staged? (ex c:\drivers\Audio)"
Get-ChildItem "$SourceFiles" -Recurse -Filter "*.inf" | ForEach-Object { PNPUtil.exe /add-driver $_.FullName /install }
}
Backup Profile
Simple scripts can really make backing up your profile a breeze. Especially with us overclockers, who tend to break their systems! While it could be argued that you can simple have your profile run off your cloud storage, or another hard drive other than the OS drive, this backup does make it easy to quickly move the profile to another device.
script coming very soon
Remove User Profile
Sometimes you want to remove profiles from a system. There are many ways to do this, but the following script is best in a network environment. This script I developed a while back, and for almost each line of code, I gave a description as to what is happening for each part. Take a look, it does a very good job of properly removing user profiles from a target system.
CODE:
Software Installs
I'll just post a single install for this, unless someone requests more. Basically, in an Enterprise environment, you may find that there are times where Enterprise Management suites don't always work as intended. A simple script could be called on to install software that is on a network share, and you can even use the repository to compare versions, without ever having to update your script! I'm working on Invoke-WebRequest commands to figure out how to do this directly with developers, but for now, it relies on someone simply updating the installer on the network drive.