Chapter 25. Additional random tips, tricks, and techniques

 

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

25.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 make the shell a little bit more customized?

25.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 PowerShell ISE, 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. Another thing that the hosting application is responsible for doing is loading and running profile scripts each time the shell starts.

These profile scripts can be used to customize the PowerShell environment, by loading snap-ins or 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 Don uses on his computer:

Import-Module ActiveDirectory
Add-PSSnapin SqlServerCmdletSnapin100
cd c:\

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

25.2. Operators: -as, -is, -replace, -join, -split, -in, -contains

25.3. String manipulation

25.4. Date manipulation

25.5. Dealing with WMI dates

25.6. Setting default parameter values

25.7. Playing with script blocks

25.8. More tips, tricks, and techniques

sitemap