Latest Posts
  • Power Tips: Tuning Script Performance

    If a script runs slow, it’s not always obvious what causes the delays, and where to optimize. With a PowerShell module called “PSProfiler” you can test how long each line in a script takes to execute. It runs both in Windows PowerShell and PowerShell 7.

    Start by installing the module…

  • Power Tips: Converting Ticks to DateTime

    Occasionally, date and time information are stored as “Ticks” in the format of a so-called “FileTime”. Ticks are 100-nanosecond units since 01/01/1601. Active Directory internally uses this format, yet you can also find it elsewhere. Here is an example reading the Windows installation time in “Ticks…

  • Power Tips: Converting UNIX Time to DateTime

    “Unix time” counts the seconds that have passed since 01/01/1970.

    For example, in Windows, you can read the installation date from the Windows Registry, and the returned value is “Unix time”:

    $values = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'…
  • Power Tips: Creating Dynamic Parameters

    Dynamic parameters are a special breed of parameters that can be shown or hidden based on runtime conditions. Your PowerShell functions could for example have just one parameter called -Action, and based on the action the user chooses, additional parameters would show. Or, you could have parameters show…

  • Power Tips: Turning Text into Individual Lines (Part 3)

    In the previous tip we split a chunk of multi-line text into individual lines and removed any empty lines.

    However, when a line isn’t really empty but contains whitespace (spaces or tabulators), it is still returned:

    # $data is a single string and contains blank lines
    $data = @'
    
    Server1
    
    …
  • Power Tips: Turning Text into Individual Lines (Part 2)

    Let’s assume your script gets text input data, and you need to split the text into individual lines. In the previous tip we suggested a number of regular expressions to do the job. But what if the input text contains blank lines?

    # $data is a single string and contains blank lines
    $data = @'…
  • Power Tips: Turning Text into Individual Lines (Part 1)

    Occasionally, you need to process multi-line text line by line. Here is an example of a multi-line string to start with:

    # working with 1-dimensional input
    
    # $data is a single string
    $data = @'
    Server1
    Server2
    Cluster4
    '@
    
    $data.GetType().FullName
    $data.Count
    

    An efficient way to split the text…

  • Power Tips: Taking Screenshot

    With types found in System.Windows.Forms, PowerShell can easily capture your screen and save the screenshot to a file. The code below captures your entire virtual screen, saves the screenshot to file, then opens the bitmap file in the associated program (if any):

    $Path = "$Env:temp\screenshot.b…
  • Power Tips: Disabling Local “Guest” Account

    Windows comes with the built-in account called “Guest”. Since this account is seldomly used, you may want to disable it. Else, its well-known name could serve as a vector for attackers.

    Since the account name is localized and can slightly vary from culture to culture, to identify the account…

  • Power Tips: Renaming Local Administrator Account

    For security reasons, you may want to consider renaming the built-in local Administrator account. This account is very powerful, and its name is easily guessable so it is a common vector for attackers. Before you rename this account, make sure you understand the consequences:

    • The account continues to…