Powershell

Exporting Data to Excel

You can easily convert object results to CSV files in PowerShell. This generates a CSV report of current processes: To open the CSV file in...

Finding Events around A Date

Often, you might want to browse all system events around a given date. Let's say a machine crashed at 08:47, and you'd like to see all...

Auto-Connecting with Public Hotspot

Many mobile phone service providers offer public hotspots at airports and public places. To connect, you typically need to browse to a logon page,...

Padding Strings Left and Right

If you must make sure that a given string has a uniform width, then you can use .NET methods to pad the string appropriately: $mytext =...

Formatting Numbers Easily

Often, users need to format numbers and limit the number of digits, or add leading zeros. There is one simple and uniform strategy for this: the...

Eliminating Duplicates

Sort-Object has an awesome feature: with the parameter -Unique, you can remove duplicates: This can be applied to object results, as well. Check out...

Beware Of Hidden Password Requests

You can run PowerShell code in any host, and Windows ships with powershell.exe and powershell_ise.exe. Many prefer the graphical ISE editor over the...

Save Time With Select-Object -First!

Select-Object has a parameter called -First that accepts a number. It will then return only the first x elements. Sounds simple, and it is. This...

Expanding Variables in Strings

To insert a variable into a string, you probably know that you can use double quotes like this: $domain = $env:USERDOMAIN $username = $env:USERNAME...

Filtering Text-Based Command Output

Comparison operators act like filters when applied to arrays. So any console command that outputs multiple text lines can be used with comparison...

Keeping a Handle to a Process

When you launch an EXE file, PowerShell will happily start it, then continue and not care about it anymore: If you'd like to keep a handle to...

Use $PSScriptRoot to Load Resources

Beginning in PowerShell 3.0, there is a new automatic variable available called $PSScriptRoot. This variable previously was only available within...

Signing VBScript Files with PowerShell

You probably know that Set-AuthenticodeSignature can be used to digitally sign PowerShell scripts. But did you know that this cmdlet can sign...

Correctly Filtering DateTime

When you use Where-Object to filter information by date or time, this works actually very well--provided you use the correct filtering format. Do...

Using Fully Qualified Names in Remoting

When you try PowerShell Remoting, you may run into connection errors just because your machine name was not fully qualified. Kerberos authentication...

Ensuring Backward Compatibility

Let's assume you have created this function: function Test-Function { param ( [Parameter(Mandatory=$true)] $ServerPath ) "You selected...

Importing Certificates from PFX Files

You can use Get-PfxCertificate to load digital certificates from PFX files, and then use the certificate to sign script files, for example: $pfxpath...

Reading StringExpand Registry Values

When you read a Registry value of type StringExpand, it will always automatically expand any environment variables contained in the text. This...

Mandatory Parameter with a Dialog

Typically, when you mark a function parameter as "mandatory", PowerShell will prompt the user when the user omits the parameter: function...

Using Comma as Decimal Delimiter

You may not be aware of this, but PowerShell uses a different decimal delimiter for input and output - which may cause confusions to script users....

Testing for Valid Date

If you need to test whether some information resembles a valid date format, here is a test function: function Test-Date { param (...

Multiple Assignments in One Line

When you assign something to a variable, you can enclose the expression in braces. This will also output the data. Have a look: $a = Get-Service ($a...

Pinging Computers

There are multiple ways how you can ping computers. Here is a simple approach that uses the traditional ping.exe but can be easily integrated into...

Playing a Sound on Error

To catch a user’s attention, your script can easily play WAV sound files. Here is a simple function: function Play-Alarm { $path =...

1 61 62 63 64 65 104