Skip to content

Instantly share code, notes, and snippets.

@drlsdee
drlsdee / Test-FileHash.ps1
Created September 19, 2024 05:46
Compares the actual hash of a file with the specified reference value using the Get-FileHash cmdlet from the Microsoft.PowerShell.Utility module.
function Test-FileHash {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory, ParameterSetName = 'Path',
Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ [System.IO.File]::Exists($_) })]
[Alias('FullName')]
[string]$Path,
@drlsdee
drlsdee / ConvertFrom-RomanNumber.ps1
Created May 16, 2024 13:03
Converts roman numbers to Int32.
function ConvertFrom-RomanNumber {
[CmdletBinding()]
[OutputType([int])]
param (
#
[Parameter(Mandatory,ValueFromPipeline,Position=0)]
[ValidatePattern('^[ivxlcdmIVXLCDM]+$')]
[string]$InputObject
)
begin {
@drlsdee
drlsdee / Get-Win32Service.ps1
Created February 13, 2024 07:10
Get-Win32Service - returns an instance of class Win32_Service including the image path with command line arguments and the service account name
function Get-Win32Service {
[CmdletBinding()]
param (
[Parameter(Mandatory,Position=0)]
[Alias('n')]
[string]
$Name,
[Parameter(Position=1)]
[ValidateSet('Name','Status','ExitCode','DesktopInteract','ErrorControl','PathName',
@drlsdee
drlsdee / Show-PSModuleCommand.ps1
Last active October 24, 2023 14:32
Simply displays a summary of the cmdlets and functions exported from the PowerShell module.
<#
.SYNOPSIS
Simply displays a summary of the cmdlets and functions exported from the PowerShell module.
.DESCRIPTION
Simply displays a summary of the cmdlets and functions exported from the PowerShell module.
.EXAMPLE
PS C:\> Get-Module -Name 'Microsoft.PowerShell.Utility' | Show-PSModuleCommand | Format-Wide -GroupBy Verb -Column 6
Displays a summary of the cmdlets and functions exported from the PowerShell module "Microsoft.PowerShell.Utility".
.INPUTS
[psmoduleinfo]
@drlsdee
drlsdee / Get-TimeStampedFileName.ps1
Created October 24, 2023 06:59
Gets an object of type System.IO.FileInfo and returns the object with generated filename based on the old name and timestamp. May be useful for creating backups, rotating logs etc.
function Get-TimeStampedFileName {
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,Position=0)]
[Alias('FullName', 'p')]
[System.IO.FileInfo]$Path,
[Parameter(Position=1)]
[Alias('u', 'z')]
@drlsdee
drlsdee / CppHexVersion.psm1
Created April 4, 2023 13:26
Two functions to convert software versions from System.Version to hex string and back
$Script:styles = [System.Globalization.NumberStyles]::AllowHexSpecifier
$Script:formats = [cultureinfo]::InvariantCulture
function ConvertTo-CppHexVersion {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias('Version')]
[version]
@drlsdee
drlsdee / Split-Array.ps1
Created April 4, 2023 13:06
Just an example of a function that splits the input string into a set of substrings of a given length.
function Split-Array {
[CmdletBinding()]
param (
[Parameter()]
[char[]]
$InputObject,
[Parameter()]
[int]
$PartSize
)
@drlsdee
drlsdee / Get-OldItem.ps1
Created March 30, 2023 09:56
Returns filesystem entries older than N days
function Get-OldItem {
[CmdletBinding()]
[OutputType([System.IO.FileSystemInfo])]
param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias('FullName')]
[System.IO.FileSystemInfo]
$Path,
[Parameter()]
[int]
@drlsdee
drlsdee / DotNetLocalizedStringFile.ps1
Created March 29, 2023 06:03
This gist includes a class and functions for checking whether a file (for example, a script or an ADMX template) has an additional file with localized data that matches the specified culture.
#Requires -Version 5.1
class DotNetLocalizedStringFile {
[System.IO.FileInfo]
$SourceFile
[cultureinfo]
$CultureInfo
[System.IO.FileInfo]
$LocalizedFile
[bool]
@drlsdee
drlsdee / Format-Guid.ps1
Created March 17, 2023 13:23
This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid. See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring
# This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid
# See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring
enum PSGuidFormatProvider {
None = 0 # Only alphanumeric characters: 00000000000000000000000000000000
Braces = 1 # Figure brackets, "{}": {00000000-0000-0000-0000-000000000000}
Dashes = 2 # Hyphens, "-": 00000000-0000-0000-0000-000000000000
Parentheses = 3 # Round brackets, "()": (00000000-0000-0000-0000-000000000000)
Xadecimal = 4 # Groups of heXadecimal values: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}
}