Skip to content

Instantly share code, notes, and snippets.

@DBremen
DBremen / RegEx.Replace.ps1
Created August 21, 2015 15:28
RegEx.Replace Examples
#Increase a number within a string by one
$data = 'XX3.txt', 'XX33.txt', 'XX333.txt'
$data | foreach{
[RegEx]::Replace($_, '(\d{1,3})', {1+$args[0].Value})
}
#Capitalize the second word within a sentence string
$script:counter = 0
[regex]::Replace('this is a test','\w+', {
$script:counter++
if ($counter -eq 2){
@DBremen
DBremen / ISECodeTemplate.ps1
Created August 24, 2015 14:55
Create default code template for PowerShell ISE
function Edit-ISETemplate{
$ISETemplatePath = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\ISETemplate.ps1"
if (!Test-Path){
New-Item $ISETemplatePath -ItemType File
}
psedit "$ISETemplatePath"
}
Register-ObjectEvent $psise.CurrentPowerShellTab.Files CollectionChanged -Action {
# files collection
@DBremen
DBremen / Get-CartesianProductArrays.ps1
Last active August 31, 2015 12:44
Returns the cartesian product for an array of arrays
#build the cartesian product for an array of arrays
function CartesianProduct($row, $currCol=0){
if ($currCol -eq 0){
$wordIndices = New-Object int[] $row.Length
}
$wordIndex = 0
#walk through the items in the current column
foreach($word in $row[$currCol]){
#add the index to the indices for the current column
$wordIndices[$currCol] = $wordIndex
@DBremen
DBremen / CartesianProduct.ps1
Created August 27, 2015 13:22
Clean up and Cross-Join excel table containing cells with multiple entries separated by comman or linebreaks
function CartesianProduct($htRow, $currCol=0){
$colCount = $htRow.Keys.Count
if ($currCol -eq 0){
$wordIndices = New-Object int[] $colCount
}
$wordCount = ($htRow.Values | select)[$currCol].Count
#walk through the items in the current
for ($wordIndex = 0; $wordIndex -lt $wordCount; $wordIndex++){
#add the index to the indices for the current column
$wordIndices[$currCol] = $wordIndex
@DBremen
DBremen / Open-Registry.ps1
Last active August 16, 2022 03:28
Description for Open-Registry.ps1
function Open-Registry{
[CmdletBinding()]
[Alias("regJump")]
param(
[Parameter(Position=0)]
$regKey
)
#check for clipbaord only if no argument provided
if (!$regKey){
#split the clipboard content by crlf and get of trailing crlf in case clipboard populated via piping it to clip.exe
@DBremen
DBremen / Wait-Window.ps1
Created September 2, 2015 12:45
Example on how to wait for a window to appear for an executable initiated from PowerShell
$regeditInstance = [Diagnostics.Process]::Start("regedit","-m")
#wait the regedit window to appear
while ($regeditInstance.MainWindowHandle -eq 0){
sleep -Milliseconds 100
}
@DBremen
DBremen / CrossJoinSheets.vb
Created September 3, 2015 19:09
VBA Excel to cross-join multiple sheets without duplicates
Sub CrossJoinSheets()
Dim cn As ADODB.Connection
Dim sql As String
Dim outputSheet As Worksheet
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ActiveWorkbook.FullName & ";" & _
@DBremen
DBremen / CleanData_CrossJoin.vb
Last active August 16, 2022 00:46
VBA Excel to cross-join and clean data with multiple entries per cell separated by comma or line-breaks
Private Function isSaved() As Boolean
Dim lastSaved As String
On Error GoTo EHandler
s = ActiveWorkbook.BuiltinDocumentProperties("last save time")
isSaved = True
Exit Function
EHandler:
isSaved = False
End Function
@DBremen
DBremen / CrossJoinRanges.vb
Created September 4, 2015 11:40
VBA Excel to cross-join multiple ranges without duplicates
Sub CrossJoinRanges()
Dim cn As ADODB.Connection
Dim sql As String
Dim outputSheet As Worksheet
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ActiveWorkbook.FullName & ";" & _
@DBremen
DBremen / PromptForChoice.ps1
Created September 10, 2015 08:19
Usage of built-in PromptForChoice
$Title = "Title"
$Info = "Pick Something!"
$options = echo Option1 Option2 Option3
$defaultchoice = 2
$selected = $host.UI.PromptForChoice($Title , $Info , $Options, $defaultchoice)
$options[$selected]