Skip to content

Instantly share code, notes, and snippets.

@mklement0
mklement0 / Touch-File.ps1
Last active September 10, 2024 14:52
PowerShell function that provides functionality similar to the Unix touch utility for updating last-modified file timestamps and creating files on demand.
<#
Prerequisites: PowerShell v5.1 and above (verified; may also work in earlier versions)
License: MIT
Author: Michael Klement <[email protected]>
DOWNLOAD and INSTANT DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/82ed8e73bb1d17c5ff7b57d958db2872/raw/Touch-File.ps1 | iex
@mklement0
mklement0 / Enter-AdminPSSession.ps1
Last active March 20, 2025 12:50
Universal PowerShell script (PSv3+) that enters an admin (elevated) session on the local computer and optionally executes commands unattended
<#
Prerequisites: PowerShell v3+
License: MIT
Author: Michael Klement <[email protected]>
DOWNLOAD and DEFINITION OF THE FUNCTION:
irm https://gist.github.com/mklement0/f726dee9f0d3d444bf58cb81fda57884/raw/Enter-AdminPSSession.ps1 | iex
@mklement0
mklement0 / IniFileHelper.psm1
Last active February 24, 2025 18:01
PowerShell module for reading and writing INI files on Windows, via the Windows API
<#
Windows-only module for reading from / updating INI files, via the Windows API.
You can either:
* download this script module (IniFileHelper.psm1) and use Import-Module to import it into a session;
If you place it in a subdirectory named IniFileHelper in in one of the directories listed in $env:PSModulePath,
it will automatically be available in future sessions.
* for ad-hoc use, download the code and create a dynamic, transient module for the
@mklement0
mklement0 / Get-HelpFile.ps1
Last active April 3, 2019 16:21
Edition-agnostic PowerShell script/function for locating help-topic source files, optionally by part of their file name.
<#
NOTE:
It is BEST TO DOT-SOURCE (.) THIS SCRIPT, which defines a function of the same
name for later use.
While it is possible to invoke this script directly, its help can then only
be invoked with the -? switch, providing only terse help,
whereas dot-sourcing provides full Get-Help integration.
Similarly, only with dot-sourcing do you get tab completion.
@mklement0
mklement0 / Invoke-Life.ps1
Created October 23, 2018 14:33
PowerShell script that implements a terminal-based version of Conway's Game of Life (see https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)
<#
.SYNOPSIS
A terminal-based implementation of Conway's Game of Life.
.DESCRIPTION
Conway's Game of Life is a zero-player game that simulates a cellular automaton.
See https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life for background.
The initial state can be seeded explicitly or, by default, randomly.
@mklement0
mklement0 / Get-InstalledProgram.ps1
Last active August 9, 2021 16:19
Windows PowerShell script that gets information about installed programs (applications), via the registry by default, optionally via the PackageManagement module
<#
.SYNOPSIS
Gets installed programs on Windows.
.DESCRIPTION
Gets installed programs (applications) on Windows, either via the registry or, optionally,
via the Get-Package cmdlet, optionally filtered by
display-name substrings.
NOTE: As of PowerShell, v7.2, -UsePackageManagement only works in Windows PowerShell
(with the PackageManagement module installed).
@mklement0
mklement0 / Invoke-WithEnvironment.ps1
Created October 10, 2018 02:27
PowerShell script/function that invokes a command with a temporarily modified environment.
function Invoke-WithEnvironment {
<#
.SYNOPSIS
Invokes commands with a temporarily modified environment.
.DESCRIPTION
Modifies environment variables temporarily based on a hashtable of values,
invokes the specified script block, then restores the previous environment.
.PARAMETER Environment
@mklement0
mklement0 / Get-SpecialFolder.ps1
Last active September 25, 2018 04:25
Get-SpecialFolder: PowerShell function that retrieves special folder (known folders), i.e., folders with special meaning to the OS.
function Get-SpecialFolder {
<#
.SYNOPSIS
Gets special (known) folders.
.DESCRIPTION
Gets items representing special folders (directories), i.e.,
folders whose purpose is predefined by the operating system.
In a string context, each such item expands to the full, literal path it
@mklement0
mklement0 / New-CsvSampleData.ps1
Last active September 25, 2018 04:26
New-CsvSampleData: PowerShell function for automated creation of sample CSV data
function New-CsvSampleData {
<#
.SYNOPSIS
Generates CSV sample data.
.DESCRIPTION
Generates simple CSV sample data as either a single multiline string or
an array of lines.
For now, the data-row field values are simply constructed as:
@mklement0
mklement0 / Get-OpenFiles.ps1
Last active April 1, 2019 18:52
Get-OpenFiles: PowerShell function for locating open files in a given directory [subtree]
function Get-OpenFiles {
<#
.SYNOPSIS
Finds open files.
.DESCRIPTION
Finds files currently being held open by any process and reports them as
[System.IO.FileInfo] instances, as Get-ChildItem would.
In fact, this function supports all parameters that Get-ChildItem does and