Chapter 18. Advanced PowerShell syntax

 

This chapter covers

  • Splatting
  • Defining default parameter values
  • Running external utilities
  • Using subexpressions
  • Using hash tables as objects

This chapter is a kind of catchall—an opportunity to share some advanced tips and tricks that you’ll see other folks using. Almost everything in this chapter can be accomplished in one or more other ways (and we’ll be sure to show you those as well), but it’s nice to know these shorter, more concise PowerShell expert techniques. These techniques save you time by enabling you to complete your tasks quicker and more easily.

18.1. Splatting

“Splatting” sounds like something a newborn baby does, right? In reality, it’s a way of wrapping up several parameters for a command and passing them to the command all at once.

For example, let’s say you wanted to run the following command:

Get-WmiObject -Class Win32_LogicalDisk -ComputerName SERVER2 `
-Filter "DriveType=3" -Credential $cred

Notice that in this command, you pass a variable, $cred, to the –Credential parameter (for this example, assume that you’ve already put a valid credential into $cred). Now, if you were doing this from the command line, splatting wouldn’t save you any time. In a script, stringing all of those parameters together can make things a little hard to read. One advantage of splatting is making that command a little prettier:

18.2. Defining default parameter values

18.3. Running external utilities

18.4. Expressions in quotes: $($cool)

18.5. Parentheticals as objects

18.6. Increase the format enumeration limit

18.7. Hash tables as objects

18.8. Summary

sitemap