Skip to content

Instantly share code, notes, and snippets.

@swbbl
swbbl / NewLogEntry.ps1
Created March 10, 2023 07:10
New-LogEntry. An easy way to create and write proper log entries
enum Severity {
INFO
WARNING
ERROR
}
class LogEntry {
[datetime] $DateTime = [datetime]::UtcNow
[Severity] $Severity
[string] $Message
@swbbl
swbbl / Get-ServiceEx_Examples.ps1
Last active December 31, 2022 08:54
Examples for PowerShell function 'Get-ServiceEx' → https://gist.github.com/swbbl/3fa226f626abfc5b29b66c2d32c920d0
# Examples for PowerShell function 'Get-ServiceEx':
# https://gist.github.com/swbbl/3fa226f626abfc5b29b66c2d32c920d0
PS> Get-ServiceEx ndis
Type : Driver
ServiceType : KernelDriver
ServiceName : NDIS
DisplayName : NDIS System Driver
Description : NDIS System Driver
@swbbl
swbbl / JoinObject.psm1
Last active January 10, 2023 07:43
Join-Object combines two collections based on property names or scriptblocks like a SQL-variant of Group-Object.
using namespace System.Collections.Generic
function Join-Object {
<#
.SYNOPSIS
Join-Object combines two collections based on property names or scriptblocks like a SQL-variant of Group-Object.
.DESCRIPTION
Join-Object combines two collections based on property names or scriptblocks like a SQL-variant of Group-Object.
@swbbl
swbbl / ImportXmlCommand.ps1
Created November 15, 2022 04:32
Import XML files using the encoding of the XML declaration (if specified) using XmlReader and XmlDocument
function Import-Xml {
[CmdletBinding(DefaultParameterSetName='Path')]
param(
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string[]]
$Path,
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath', 'LP')]
@swbbl
swbbl / Get-ServiceEx.ps1
Last active April 9, 2023 10:17
Gets all services and system/kernel drivers with all important details (more than Get-Service)
# usage:
# Get service 'DHCP':
# PS> Get-ServiceEx dhcp
#
# Get kernel driver 'ndis':
# PS> Get-ServiceEx ndis
#
# Get all services which are running via svchost:
# PS> Get-ServiceEx -ProcessName svchost
#
@swbbl
swbbl / _backupNcopy.ps1
Last active February 9, 2024 06:24
Backup/Copy files and folders while remaining folder structure
# Place the file into the source root directory. All files will be recursively copied to target directory.
$ErrorActionPreference = 'STOP'
$sourceRootDirPath = $PSScriptRoot
# Set the target directory. Ensure that you don't select a parent directory.
# If you place the script to e.g. "C:\ThisIsMySource", you must provide "ThisIsMySourceDirectory" in the target directory path -> "D:\ThisIsMySourceDirectory"
# Don't use just "D:\"
$targetRootDirPath = 'E:\Games\Steam\steamapps\common\' + ([System.IO.DirectoryInfo]$sourceRootDirPath).Name
# if $useFileChecksum is $false, "LastWriteTimeUtc" and "Size" is used.
function Get-ComputerSession {
[CmdletBinding()]
param(
# The computer name to get the current sessions from. Use an empty string for local machine.
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[AllowEmptyString()]
[String]
$ComputerName,
# Flattens the output