Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / Get-FileInvokeWebRequest.ps1
Last active October 21, 2015 08:44
Download files using Invoke-WebRequest
function Get-FileInvokeWebRequest{
param(
[Parameter(Mandatory=$true)]
$url,
$destinationFolder="$env:USERPROFILE\Downloads",
[switch]$includeStats
)
$destination = Join-Path $destinationFolder ($url | Split-Path -Leaf)
$start = Get-Date
Invoke-WebRequest $url -OutFile $destination
@DBremen
DBremen / Get-FileBitsTransferSynchronous.ps1
Last active October 27, 2015 12:58
Download files using Start-BitsTransfer in synchronous mode
function Get-FileBitsTransferSynchronous{
param(
[Parameter(Mandatory=$true)]
$url,
$destinationFolder="$env:USERPROFILE\Downloads",
[switch]$includeStats
)
Import-Module BitsTransfer
$destination = Join-Path $destinationFolder ($url | Split-Path -Leaf)
$start = Get-Date
@DBremen
DBremen / Get-Uninstaller.ps1
Last active February 14, 2016 19:27
Retrieve Uninstall information for software packages from the registry
function Get-Uninstaller {
[CmdletBinding()]
Param(
$DisplayName = '*',
[ValidateSet('HKLM', 'HKCU')]
[string[]]$Hive = @('HKLM','HKCU')
)
$keys =@'
:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall
@DBremen
DBremen / Get-Uninstallerv5
Created February 14, 2016 19:30
Retrieve Uninstall information for software packages via Get-Package PowerShell v5
#requires -Version 5
Get-Package | foreach{
$attributes = $_.meta.attributes
$htProps = [Ordered]@{msiGUID=''}
for ($i=0;$i -lt $attributes.keys.count;$i++){
$htProps.Add($attributes.keys[$i].LocalName,$attributes.values[$i])
}
if ($htProps.Contains('DisplayName')){
$uninstallString = $htProps.UninstallString
$modifyPath = $htProps.ModifyPath
function Get-RandomUser {
<#
.SYNOPSIS
Generate random user data.
.DESCRIPTION
This function uses the free API for generating random user data from https://randomuser.me/
.EXAMPLE
Get-RandomUser 10
.EXAMPLE
Get-RandomUser -Amount 25 -Nationality us,gb -Format csv -ExludeFields picture
@DBremen
DBremen / test.ps1
Created October 12, 2016 21:54
Matrix using hashtables
$htUsers = @{}
$htProps = @{}
$addADGroupMembers.Keys | foreach {$htProps.$_=$null}
foreach ($group in $addADGroupMembers.GetEnumerator()){
foreach ($user in $group.Value){
if (!$htUsers.ContainsKey($user)){
$htProps.UserID = $user
$htUsers.$user = $htProps.Clone()
}
($htUsers.$user).$($group.Name) = 1