Creating a PowerShell Profile
What is PowerShell?
I spent my earliest days in IT clicking through Active Directory, opening accounts individually, viewing the `LastLogonTimeStamp`, and counting back 90 days. If the account didn’t log in for 90 days, I would right-click the account, deactivate the account, and move it to an Organizational Unit for inactive user objects. This process that occurred every 90 days took a couple of days to dig through a few thousand accounts. This process was extraordinarily mundane. I felt like my eyes were bleeding. Halfway into the second round of an inactive users audit, I thought there had to be a way.
That’s when I ran into PowerShell. I found a magic line that did the trick.
Get-ADUser -Filter {(LastLogonDate -lt (Get-Date).AddDays(-90)) -and (Enabled -eq $true)} -Properties LastLogonDate | ForEach-Object {Disable-ADAccount $_; Move-ADObject -Identity $_ -TargetPath "OU=InactiveUsers,DC=YourDomain,DC=com"}
I could not believe my eyes; days of dreadfully boring work were complete!
Microsoft developed PowerShell in November 2006 for configuration management and task automation. It is a command-line shell and a scripting language for Windows desktop and server operating systems. In 2016, PowerShell Core was released, which allowed other operating systems like macOS and Linux to run PowerShell.
Here are some features that I have found to be enormously helpful and why I have stuck with PowerShell over the decade:
It’s Object-Oriented, which makes it extremely easy to manipulate data
PowerShell has a ton of built-in Cmdlets, which are .NET classes, that make administration incredibly easy
The wonderful Pipeline allows the output of one cmdlet to be passed to the following, enabling complex operations to happen in one line
Scripting (.ps1) allows for repetitive tasks to be made more accessible, especially when paired with Crons or Tasks
Why Create a PowerShell Profile?
If you are familiar with shells in Linux and macOS, then an easy comparison is to think of a PowerShell profile like your .zshrc or .bashrc. If you aren’t familiar with .zshrc or .bashrc, the following information in the section will likely be helpful.
A PowerShell profile is a script that runs every time you start a new PowerShell session. It enables you to customize your PowerShell environment, like preloading modules, defining variables, aliases, functions, or even a joke of the day, allowing you to increase productivity in many ways.
How Do I Create a PowerShell Profile?
On Windows:
Open PowerShell by going to Start -> type ‘powershell.exe’ -> hit ‘enter’
See if there is currently a PowerShell profile running by running the following:
’Test-Path $PROFILE’
If the above command returns False, this means you don’t have a profile yet
Create a new profile by running the following:
’New-Item -Path $PROFILE -ItemType File -Force’This will look as follows:
As you can see, it created a file in the C:\Users\<your_username>\Documents\WindowsPowerShell directory
Next, you will want to open the profile for editing. You can do this by opening the file in your favorite IDE (what I will do), or you can run the following to open up Notepad for editing:
notepad $PROFILE
In the PowerShell Profile, we will do the following:
Import a Module
Set an alias
Add a function
Change the prompt
Output a dad joke
For Importing a module, we will import the PSWorkflow module by adding the following code to your PowerShell profile in your editor
# Import Modules Import-Module PSWorkflow
Next, we are going to set ‘ll’ as an alias for ‘Get-ChildItem’ to list directory contents, similar to macOS and Linux, by adding the following code to your profile
# Set Aliases Set-Alias ll Get-ChildItem
We are going to add a ‘Get-DateTime’ function, as for some reason, this doesn’t exist yet, and it will save us time from constantly converting time by adding the following code to your profile
# Functions function Get-DateTime { Get-Date -Format "yyyy-MM-dd HH:mm:ss" }
We are going to change the prompt to “PS> “ by adding the following to the profile
function prompt { "PS> " }
Just for fun, we are going to add outputting a dad joke every time you enter a new PowerShell session; add the code to your editor:
# Just for Fun function Get-DadJoke { $headers = @{ Accept = 'application/json' UserAgent = 'PowerShell' } try { $response = Invoke-RestMethod -Uri "https://icanhazdadjoke.com/" -Headers $headers Write-Host "Dad Joke: $($response.joke)" -ForegroundColor Cyan } catch { Write-Host "Couldn't fetch a dad joke. Maybe next time!" -ForegroundColor Red } } # Call the function to display a joke at startup Get-DadJoke
After all the edits, your Microsoft.PowerShell_profile.ps1 will look as follows:
Save your changes and reopen PowerShell
Profit.