In the previous tip we explained how you can use a separate new and fresh PowerShell to retrieve all default variables. When you examine these variables closely, you will discover that still some PowerShell variables are missing.
Here is a slightly modified version called Get-BuiltInPSVariable that includes all reserved PowerShell variables:
function Get - BuiltInPSVariable ( $Name = ' * ' )
{
# create a...

Sometimes it would be useful to identify the automatic PowerShell variables managed by PowerShell so you could differentiate between built-in variables and your own. Get-Variable always dumps all variables.
Here is a simple trick that uses a separate new and fresh PowerShell runspace to determine the built-in PowerShell variables:
# create a new PowerShell
$ps = [ PowerShell ] :: Create ()
# get all variables inside...

Starting in PowerShell 5, you can define PowerShell classes. They are defined dynamically and live in memory. So how would you know the names of classes that have been defined?
Let’s first define a really simple class that does not do anything:
class TestClass
{
}
How would you be able to check whether a class named “TestClass” exists in memory? Here is a helper function called Get-PSClass...

Starting with PowerShell 5, you can define classes. They have many use cases. One would be to create libraries of useful helper functions to better organize them. For this, a class would define “static” methods. Here is a simple example:
class HelperStuff
{
# get first character of string and throw exception
# when string is empty or multi-line
static [ char ] GetFirstCharacter ([ string ] $Text )
...

In the previous tip we explained how you can add a personal greeting to your PowerShell profile. This greeting can also be spoken out, provided your volume is turned up. This works for all Powershell hosts including VSCode.
This will add the code to your profile script:
# create profile if it does not yet exist
$exists = Test-Path -Path $Profile . CurrentUserAllHosts
if ( ! $exists )
{
$null = New-Item -Path...

Here is a simple approach that takes an array of strings and returns a random string that you could use for custom greetings in PowerShell:
$greetings =
' Hello there! ' ,
' Glad to see you! ' ,
' Happy coding! ' ,
' Have a great day! ' ,
' May the PowerShell be with you! '
$greetings | Get-Random
All you need to do is add the code to your profile script, for...

Lately there have been reports about using Linq, a .NET query language, with PowerShell in an effort to speed up code.
Until there is true Linq support in PowerShell, using Linq is very cumbersome and requires strict typing and access to undocumented methods. What’s more, the same can be achieved with pure PowerShell approaches, and the speed increase often is just marginal – at least not very relevant to...

When you open a native PowerShell console in Windows 10, you can hold down CTRL+SHIFT, then move your mouse wheel, to adjust console background color transparency, and let other windows shine through. The same works for cmd.exe as well, of course.
ReTweet this Tip!

Windows is using a DNS cache, so when you change DNS servers, you need to flush the cache before the new settings take effect. PowerShell is a friend with old console commands, so you can simply run this line in PowerShell:
PS> ipconfig /flushdns
ReTweet this Tip!

PowerShell is a friend with old console commands, so the easiest way of displaying the tree structure of a folder is the old “tree” command. It works best in a native PowerShell console because editors often use a different character set. Try this:
PS> Tree $home
Just make sure you are running this command in a native PowerShell console or VSCode. You can then pipe the result to clip.exe and paste...

In the previous tip we explained how you can download port assignments via PowerShell from IANA. This process requires Internet access and can take a while. So here is code that looks for a cached CSV file. If it is present, port data is loaded from file offline, else the data is retrieved online, and a cache file is written. Note especially how Tee-Object is used to create the cache file:
$url = ' https://www.iana...

While you need administrative privileges to adjust time and date on your computer, each user can change the time zone, i.e. when you travel. PowerShell 5 comes with a very simple family of cmdlets to manage time zones. First, check out your current setting:
PS> Get-TimeZone
Id : W. Europe Standard Time
DisplayName : (UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien
StandardName : Mitteleuropäische...

If you need to run a PowerShell script in regular intervals, why not run it as a scheduled task? Here are some lines that help you create a new scheduled task to run a PowerShell script at 6AM:
#requires -Modules ScheduledTasks
#requires -Version 3.0
#requires -RunAsAdministrator
$TaskName = ' RunPSScriptAt6 '
$User = "train\tweltner"
$scriptPath = "\\Server01\Scripts\find-newaduser.ps1"...

In a PowerShell console, you can continue to pipe to more, just like in cmd.exe, to view results page by page. However, more does not support real-time pipelining, so all data needs to be collected first. This can take a long time and burn much memory:
dir c:\windows -Recurse -ea 0 | more
A better way is to use PowerShell’s own paging mechanism:
dir c:\windows -Recurse -ea 0 | Out-Host -Paging
Note that...