With PowerShell, it's easy to calculate the difference between dates:
$StartDate = Get-Date
$EndDate = [datetime]"10/31/2018"
New-TimeSpan -Start $StartDate -End $EndDate
Thus, we can see that it's over 192 days until the next Halloween.
Saturday, April 21, 2018
Tuesday, April 3, 2018
Turn Complex Commands into Functions or Scripts
We've been crafting a complicated but powerful command to get the top ten CPU-eating processes on a computer, presented in a nice readable format:
Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize
But who wants to type that ugly command repeatedly, or can even remember it from one day to the next?
Fortunately, PowerShell makes it easy to execute it using two methods. The first is to wrap the long command inside a function:
Function Get-TopCPU {
Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize
}
Run all that in your PowerShell window, and thereafter we can get the information by simply typing:
Get-TopCPU
There. That was easy.
Unfortunately, the function will be lost as soon as we close the PowerShell window. If we want to keep a custom function permanently, we can add it to our PowerShell profile. (Enter 'notepad $profile' for a quick edit.)
Another way to keep the long ugly command is to enclose the command in quotes and redirect it to a script:
' Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize ' > Get-TopCPU.ps1
Then, for as long as we have the script saved somewhere convenient, we can always launch it:
Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize
But who wants to type that ugly command repeatedly, or can even remember it from one day to the next?
Fortunately, PowerShell makes it easy to execute it using two methods. The first is to wrap the long command inside a function:
Function Get-TopCPU {
Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize
}
Run all that in your PowerShell window, and thereafter we can get the information by simply typing:
Get-TopCPU
There. That was easy.
Unfortunately, the function will be lost as soon as we close the PowerShell window. If we want to keep a custom function permanently, we can add it to our PowerShell profile. (Enter 'notepad $profile' for a quick edit.)
Another way to keep the long ugly command is to enclose the command in quotes and redirect it to a script:
' Get-Process | Sort-Object CPU -Descending | Select-Object `
-First 10 | Format-Table Name,@{Name="CPU(Min)";Expression` ={"{0:F2}" -f($_.CPU/60)}} -Autosize ' > Get-TopCPU.ps1
Then, for as long as we have the script saved somewhere convenient, we can always launch it:
Subscribe to:
Posts (Atom)