Category Archives: QuickTip

Quicktip: Create an object that refers to its own properties

Recently I received the question the /r/PowerShell community if it was possible to create an object that refers to its own properties when it is created. The user specified that they would prefer to create this as a one-liner.

Using the Add-Member cmdlet in combination with the -Passthru parameter this is quite simple to do, for example:

1
2
3
$Object = New-Object PSObject -property @{
    Property1 = 'Hello World'
} | Add-Member ScriptProperty GetProperty1 {$This.Property1} -PassThru

To make this slightly more interesting it is also possible to manipulate this object by using the manipulating the property. In the following example a property ‘Today’ is created and the script property uses this property to calculate the next week. For example:

1
2
3
$Object = New-Object PSObject -Property @{
        Today = (Get-Date).Date
} | Add-Member ScriptProperty NextWeek {($This.Today).AddDays(7)} -PassThru

ScriptPropertyNextWeek

It is also possible to create a ScriptMethod that uses the previously defined property, in the next example I create a ScriptMethod that can add or substract a week from the Today property:

1
2
3
4
$Object = New-Object PSObject -Property @{
        Today = (Get-Date).Date
} | Add-Member ScriptProperty NextWeek {($This.Today).AddDays(7)} -PassThru |
Add-Member ScriptMethod AddWeeks {param([int]$Week) $This.Today.AddDays($Week*7)} -PassThru

ScriptMethodAddWeeks

Share

QuickTip: Pin PowerShell to the Taskbar

When you are working with PowerShell in your daily work it might be worth considering pinning PowerShell to the taskbar. By doing this you have easy access using either the the mouse of the keyboard hotkeys.

PinPowerShell

By placing PowerShell in the first location on the taskbar you will have the following hotkeys available:

Description of action Key Combination
Open new (non-administrative) PowerShell console if not open already, otherwise cycle through open consoles <WIN> 1
Open new Administrative PowerShell console if not open already, otherwise cycle through open consoles <CTRL> <SHIFT><WIN> 1
Open new (non-administrative) PowerShell console even if a console is already open <WIN> <SHIFT> 1
Open the context menu, allows for opening:

  • PowerShell as Administrator
  • PowerShell
  • ISE as Administrator
  • ISE
<WIN> <ALT> 1

Using the mouse you have the following, similar, options available to you. Similar to the keyboard combinations:

Description of action Mouse Combination
Open new (non-administrative) PowerShell console if not open already, otherwise cycle through open consoles Click
Open new (non-administrative) PowerShell console even if a console is already open <SHIFT> Click
Open new Administrative PowerShell console even if a console is already open <CTRL> <SHIFT> Click
Open the context menu, allows for opening:

  • PowerShell as Administrator
  • PowerShell
  • ISE as Administrator
  • ISE
RightClick

Using these key combinations allows you to access PowerShell faster and no mouse-clicking will be required anymore.

Share

QuickTip: Select all links using Invoke-WebRequest

Today I was looking for a way to discover all links on my blog. Invoke-WebRequest returns a number of interesting properties, which can be listed by piping the output into the Get-Member cmdlet:

1
2
Invoke-WebRequest -Uri 'www.jaapbrasser.com' |
Get-Member -MemberType Property

The output from the previous command shows that there is a property named ‘Links’, we will use this property to retrieve all the urls from a site. For example by executing the following code:

1
(Invoke-WebRequest -Uri 'www.jaapbrasser.com').Links.Href

To ensure that only the unique links are selected Select-Object -Unique can be used:

1
2
(Invoke-WebRequest -Uri 'www.jaapbrasser.com').Links.Href |
Select-Object -Unique

Invoke-WebRequest

Share

QuickTip: Determine current line number in PowerShell

When scripting it can be interesting to know which line of the script is currently being executed, to determine this we take a look at the $Myinvocation automatic variable. This variable contains the ScriptLineNumber property which can be used what the current line of the script is. We can try run the following code:

1
2
PS > $Myinvocation.ScriptlineNumber
0

Although the output is correct this is not the output we are looking for. In order to display the line numbers for a script or function the variable should be placed in a function. The reason for this is that it determines from which line its function has been invoke, it cannot determine that if accessed directly. The following code is :

1
2
3
4
5
6
7
Function Get-CurrentLine {
    $Myinvocation.ScriptlineNumber
}
# Some comments
Get-CurrentLine
$null
Get-CurrentLine

This will output the numbers 5 and 7 which correspond to the line numbers. This can be useful when implementing the Write-Debug and Write-Verbose parameters in your scripts or functions in order to establish at what position in the script you currently are.

Automatic Variables
Get-Help about_Automatic_Variables
Share

QuickTip: Update Windows Defender definitions using Update-MPSignature

Since protecting your computer is as important as anything it might be nice to know that there is also a PowerShell cmdlet available to manually update your virus and malware definitions:

Update-MPSignature

So for example if you would like your Windows Defender definitions to be updated every time a new PowerShell window is opened the following code could be added to your PowerShell profile:

Update-MPSignature -AsJob

I added in the -AsJob so the updating will take place in the background and PowerShell is immediately available to use. To add this to your current PowerShell profile you could execute the following code:

Add-Content -Path $Profile -Value "`r`nUpdate-MPSignature -AsJob`r`n"

The next time you launch PowerShell it will automatically update your definitions:

Update-MPSignature

Share