Skip to content

Instantly share code, notes, and snippets.

@winkler-winsen
winkler-winsen / Copy-FilesRecurseWithFilter.ps1
Created April 26, 2024 09:28
PowerShell copy files with filter and maintain directory structure
$sourceDir = 'C:\SourceDir\'
$targetDir = 'C:\DestDir\'
Get-ChildItem $sourceDir -Filter "*" -Recurse |`
Where-Object {$_.LastWriteTime -gt (Get-Date).addDays(-1) -and -not $_.PSIsContainer} |`
foreach {
$targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
@winkler-winsen
winkler-winsen / Reset-ComPorts.ps1
Last active February 17, 2025 15:32
Reset Windows COM port settings and remove hidden / non active devices
# Reset Windows COM port settings
# 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter' ComDB
$path='HKLM:\SYSTEM\CurrentControlSet\Control\COM Name Arbiter'
Set-ItemProperty -Path $path -Name Test -Value ([byte[]](0x01)) -Verbose
# 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports'
$path='HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports'
Get-ItemProperty $path | Get-Member -MemberType NoteProperty | where Name -like 'COM*' | foreach { Remove-ItemProperty -Path $path -Name $_.Name -Verbose }
@winkler-winsen
winkler-winsen / Check-GPOResources.ps1
Created March 13, 2024 14:43
Check GPO files on SYSVOL for every .adm/.admx file ha a correspondant resource .adml file in language sub dir.
# check GPO files on SYSVOL for every .adm and .admx file has a correspondant resource .adml file in language sub dir.
# https://learn.microsoft.com/en-us/troubleshoot/windows-server/group-policy/create-central-store-domain-controller
#
$p='\\ad.contoso.local\sysvol\ad.contoso.local\Policies\PolicyDefinitions'
$langdirs= Get-ChildItem -Path $p -Directory -Filter '*-*'
Set-Location -Path $p
$adm = Get-ChildItem -Path $p -File
@winkler-winsen
winkler-winsen / ExportRegExGroupsTo-CSV.ps1
Last active January 10, 2024 11:09
PowerShell export RegEx matches with named capture groups to CSV
$LogPath='C:\Temp'
$LogPath+='\'
$LogFilename='Filename.log'
# Use RegEx named capture groups for automatic PropertyName e.g. (?<GroupName>.+)
$LogPattern='^(?<date>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})\s(?<level>[A-Z]+)\s(?<component>\[[\w.]+\])\s\((?<object>\S+)\)\s(?<exception>[\w\s:.\(\)]+)\s\[(?<eceptionid>\d+)\]\s(?<error>\w+)\s(?<errorid>[-\d]+)'
$A=Select-String -Pattern $LogPattern -Path $LogPath$LogFilename
$Output = @()
foreach ($M in $A.Matches) {
@winkler-winsen
winkler-winsen / Get-ActiveNetConnections.ps1
Last active December 5, 2023 16:17
Shows established TCP ports and its connected hostnames with local process names
# Lists all active local TCP ports and ask which to check for connections
# https://gist.github.com/winkler-winsen/f8f7ac2d25bae2c92b05ce20389b2d1a
(Get-NetTCPConnection -State Established |
Where-Object -FilterScript { $_.LocalAddress -notlike '127.0.0.1' -and $_.LocalAddress -notlike '::1' } |
Sort-Object -Unique LocalPort |
Select-Object @{L="Proto"; E={"TCP"}},
LocalAddress,
LocalPort,
@{L="Connections"; E={((Get-NetTCPConnection -State Established -LocalPort $_.LocalPort).Count)}},
@winkler-winsen
winkler-winsen / Modify-IniFiles.ps1
Created October 5, 2023 11:44
PowerShell INI file input output
# source: https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/
# alternatively you can use PsINI Module https://www.powershellgallery.com/packages/PsIni/3.1.2
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
^\[(.+)\] # Section
{
@winkler-winsen
winkler-winsen / Backup-SQLDbs.ps1
Last active September 8, 2023 12:12
MS SQL Server backup databases PowerShell script
# https://learn.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module?view=sql-server-ver16
$creddi=Get-Credential # DB user credentials
$sqlsrv='ITK-AG2' # DB server
$dbnames='itkh_*' # DB table search pattern or just the name
$share='\\PC7231\Freigabe\' # Network share for backup place. SQL server must have write rights onto it
$now=Get-Date -Format 'yyyymmdd_HHmmss'
Get-SqlDatabase -ServerInstance $sqlsrv |
Where-Object -FilterScript { $_.Name -like $dbnames} |
ForEach-Object {
Backup-SqlDatabase -Credential $creddi -ServerInstance $sqlsrv -Database $_.Name -CopyOnly -BackupFile "$share$now.$sqlsrv.$($_.Name).sqlbak"
@winkler-winsen
winkler-winsen / Get-NetListenPID.ps1
Last active September 28, 2023 10:19
PowerShell netstat -anob replacement. Shows listening TCP and UDP ports and its process names
(Get-NetUDPEndpoint |
Select-Object @{L="Proto"; E={"UDP"}},
LocalAddress,
LocalPort,
OwningProcess,
@{L="Process"; E={((Get-Process -ID $_.OwningProcess).Name)}},
@{L="Path"; E={((Get-Process -ID $_.OwningProcess).Path)}}) +
(Get-NetTCPConnection -State Listen |
Select-Object @{L="Proto"; E={"TCP"}},
LocalAddress,
@winkler-winsen
winkler-winsen / Get-ChildItemVsCmdDir.ps1
Created August 4, 2023 14:48
PowerShell: compare Get-ChildItem vs. cmd.exe dir
$Path='C:\'
$SearchFiles=@(
'.exe',
'.dll'
)
Write-Host "Suche Dateien..."
Write-Host "--------- GCI -------------"
Measure-Command {
@winkler-winsen
winkler-winsen / LogRoatate.ps1
Created June 6, 2023 10:59
Log rotation afet x days / delete files older than x days
$LogPath = "E:\"
$LogMaxDays = -30
$CurrentDate = Get-Date
$DateToDelete = $CurrentDate.Date.AddDays($LogMaxDays)
Get-ChildItem -Path $LogPath -File -Filter '*.log' | Where-Object LastWriteTime -LT $DateToDelete | Remove-Item -Confirm