Powershell

Why GetTempFileName() is Evil

Some may have come across a .NET call that is supposed to generate random temporary file names: $path = [System.IO.Path]::GetTempFileName() $path...

Adding ValidateRange to a Variable

If you'd like to apply a numeric range of legal values to a variable, you can add a ValidateRange attribute to the variable, pretty much like...

Why $MaximumHistoryCount has a Limit

If you try and increase your maximum command history, you may run into some limitations: PS C:\> $MaximumHistoryCount = 100000 The variable...

Increase History Cache

Command history can be a great help when you work for a while in a PowerShell session. Each session stores the commands you issued, and you can...

Getting Last Bootup Time

In PowerShell 3.0 and better, it's trivial to get back real DateTime information from WMI using Get-CimInstance. This would tell you when your...

Copying Arrays (Part 2)

In a previous tip we explained how you can safely "clone" an array using Clone() method. This will copy the content of an array to a new...

Copying Arrays (Part 1)

When you copy variable content, you may just copy the "reference" (memory address), not the content. Take a look at this example: $a =...

Using Encoded Scripts

In VBScript there were encoded scripts. Encoding is by no means a safe way of hiding script content, but it makes it a little harder for users to...

Using Background Jobs

Background jobs can help speed up your scripts. If your scripts consist of a number of separate tasks that also could run in parallel, then...

Removing Windows 10 Apps

Windows 10 comes with a bunch of preinstalled apps. Fortunately, you can use PowerShell to remove any app you don’t like. You may need...

Fixing Remoting Sender Information

If you use Invoke-Command to remotely execute PowerShell code, you may have noticed that PowerShell remoting adds a new PSComputerName property that...

Setting New Windows Registered Owner

This small piece of code prompts for a new registered owner name, then updates the value in the Windows Registry. Note that this requires...

Downloading Files

Invoke-WebRequest can download files from the internet for you. This example downloads a 33MB public NASA video to your computer, then starts to...

Why Invoke-Expression is Evil

Invoke-Expression takes any string and treats it as PowerShell code. This way, you could construct dynamic code, and then execute it....

Accessing Web Page Content

Beginning with PowerShell 3.0, the cmdlet Invoke-WebRequest can download web page content quite easily. This would scrape all links from...

Analyzing svchost Processes

Occasionally, you may see a bunch of processes named "svchost" in your task monitor or Get-Process output. These processes are hosts for Windows...

Identifying Services by ProcessID

Group-Object is a great cmdlet to create lookup tables. If you wanted to identify a Windows service by its process ID, here is a way: $serviceList =...

Creating Real Classes

PowerShell introduces class support in PowerShell 5.0, but you can define your own classes in other PowerShell versions as well. Simply use C# code...

Adding Test Hosts to PowerShell ISE

To quickly open new test hosts inside the PowerShell ISE that ships with PowerShell 3.0 and better, here is a small helper function: #requires...

Defining Default Parameters

PowerShell can define defaults for any parameter, so if you wanted to always submit a default value for Get-ChildItem‘s –Path parameter,...

Creating New Objects – Oneliner

Sometimes you may want to create your own objects to store multiple pieces of information. Here is a pretty dense oneliner that illustrates a quick...

Mapping Network Drives (Part 3)

If you migrated from VBScript to PowerShell, you may remember how VBScript mapped network drives. This option is still available in PowerShell....

1 48 49 50 51 52 104