Tuesday, February 27, 2018

Start a Hidden Process with PowerShell

Here's how to launch a hidden Windows process using PowerShell:

 Start-Process -WindowStyle hidden -FilePath <process_name>

 


 When we run this, we won't see anything happen. That's because the notepad window is hidden. But if we check Task Manager we'll see that notepad actually launched.

Sunday, February 18, 2018

Loop Through Repetitive Commands

Here's an example I uncovered at my day job which saved me a lot of time and tedium.

The Storage team asked me to get disk information from a cluster pair. What they wanted was for me to run the following command at the command line:

mpclaim -s -d #

Where '#' was the number of the disk. The two cluster nodes have 18 disks each, so that would involve running that command 36 times.

Pfft on that. PowerShell to the rescue.

First, I created an array and filled it with the numbers 0 through 17, the actual disk numbers:

$array = ( 0..17 )

 


Then I used a Foreach loop to loop through the 'mpclaim' command:

Foreach ($unit in $array) {
     mpclaim -s -d $unit | Out-File C:\Temp\mpio.txt -Append
}







Essentially, one by one, each number in the array gets assigned to the variable $unit, then plugged into the 'mpclaim' command. The output is piped to a text file using Out-File, and since I added the -Append parameter, the last results are added to the end of the file. Otherwise, the previous results would have been overwritten. 

The amount of time it took for PowerShell to put my data into one neat text file to send to the Storage team? Two seconds. (I counted.)

























Tuesday, February 13, 2018

Get Module Commands

In PowerShell, modules are packages of commands, functions, and scripts, often around a theme, like the Registry or Active Directory. To see what modules are loaded in our PowerShell session, use the Get-Module cmdlet

Get-Module




There may be other modules available but which haven't been imported yet. We can add the -ListAvailable switch to see them:

Get-Module -ListAvailable




To see what commands come with a module, we can use the Get-Command cmdlet and specify the module:

Get-Command -Module <module-name>

 
  
 
Once we've decided we want to actually use the commands in the module, we can Import them with Import-Module:

Import-Module -Name <module-name>

 
 

Sunday, February 11, 2018

Get Aliases for Often Used Commands

PowerShell provides aliases for many commands to save on typing. For example, the cmdlet "Get-Service" can be called with the alias 'gsv'.

But how can we know if a particular command or cmdlet even has an alias? Two ways:

First, use "Get-Alias" to see the entire list of aliases available:

Get-Alias

















Second, to save scrolling and squinting, we can narrow down that long list of aliases by using the -Definition switch for the command we want to investigate:

Get-Alias -Definition <cmdlet>



Scripter's Note: Aliases are useful for working on the command line, but when writing scripts, it's best practices to use the full command names. This makes it easier for yourself and for others to read the script.

Monday, February 5, 2018

Find the Day of the Year

The easiest way to use PowerShell to get the date is--surprise!--Get-Date:

Get-Date

 


But that output is not a string of text. It's an object, with properties that can be searched for, extracted, and manipulated. For example, how many days into the year are we on today's date?

(Get-Date).DayOfYear

 


My how the days fly by.