26 Tips, tricks, and techniques

 

We’re nearing the end of your month of lunches, so we’d like to share a few random extra tips and techniques to round out your education.

26.1 Profiles, prompts, and colors: Customizing the shell

Every PowerShell session starts out the same: the same aliases, the same PSDrives, the same colors, and so forth. Why not customize the shell a little bit more?

26.1.1 PowerShell profiles

We’ve explained before that there’s a difference between a PowerShell hosting application and the PowerShell engine itself. A hosting application, such as the console or the VS Code, is a way for you to send commands to the PowerShell engine. The engine executes your commands, and the hosting application is responsible for displaying the results. The hosting application is also responsible for loading and running profile scripts each time the shell starts.

These profile scripts can be used to customize the PowerShell environment by loading modules, changing to a different starting directory, defining functions that you’ll want to use, and so forth. For example, here is the profile script that Sarah uses on her computer:

Import-Module ActiveDirectory
Import-Module DBATools
cd c:\

The profile loads the two modules that Sarah uses the most, and it changes to the root of her C: drive, which is where Sarah likes to begin working. You can put any commands you like into your profile.

26.1.2 Customizing the prompt

26.1.3 Tweaking colors

26.2 Operators: -as, -is, -replace, -join, -split, -contains, -in

26.2.1 -as and -is

26.2.2 -replace

26.2.3 -join and -split

26.2.4 -contains and -in

26.3 String manipulation

26.4 Date manipulation

26.5 Dealing with WMI dates