Skip to content

Instantly share code, notes, and snippets.

@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
@DBremen
DBremen / Clear-Clipboard.ps1
Last active August 16, 2022 00:47
Function to identify and stop/restart process blocking the clipboard
function Restart-Process{
[CmdletBinding()]
param([Parameter(ValueFromPipeline = $true)] $process)
Begin{
$selectedIndex=$multiInstanceName=$null
}
Process{
if ($multiInstanceName -eq $_.Name){continue}
$procToRestart=$_
#handle if there are multiple instances of the same application
@DBremen
DBremen / bing.ps1
Last active August 16, 2022 00:48
Usage of Bing Search and Synonyms API through PowerShell
#https://datamarket.azure.com/account/keys
#Home -> My Account -> Account Keys:
#based on http://www.powershelladmin.com/wiki/Accessing_the_Bing_Search_API_v2_using_PowerShell
function Get-BingSearchResult($query){
Add-Type -Assembly System.Web
$Key = 'YOURACCOUNTKEY'
$Base64KeyBytes = [byte[]] [Text.Encoding]::ASCII.GetBytes("ignored:$Key")
$Base64Key = [Convert]::ToBase64String($Base64KeyBytes)
$QueryString = '%27' + [Web.HttpUtility]::UrlEncode($query) + '%27'
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 / ConvertFrom-ExcelClipboard.ps1
Last active August 16, 2022 00:46
Convert copied range from excel to an array of PSObjects
function ConvertFrom-ExcelClipboard {
<#
.SYNOPSIS
Convert copied range from excel to an array of PSObjects
.DESCRIPTION
A range of cells copied into the clipboard is converted into PSObject taking the first row (or provided property names via Header parameter) as the properties.
.EXAMPLE
#Considering a range of cells including header has been copied to the clipboard
ConvertFrom-ExcelClipboard
.EXAMPLE
@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
function New-PowershellWebGUI ($HTMLRaw,$Title,$Runspace) {
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="$Title" Height="500" Width="700">
<Grid>
<DockPanel>
<WebBrowser Name="WebBrowser" DockPanel.Dock="Top" Margin="30">
</WebBrowser>
Function Show-Diff {
param([String[]]$OldString,[String[]]$NewString)
$WebPage = @"
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
function Show-HTML ([string]$HTML){
Add-Type –AssemblyName PresentationFramework
if(Test-Path $HTML -IsValid){
$txt = Get-Content $HTML -Raw
$HTML = $txt
}
$HTML.Replace('<head>','<meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta http-equiv="Content-Type" content="text/html;charset=utf-8">')
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Function New-PSWebServer {
<#
.Synopsis
Creates a web server that will invoke PowerShell code based on routes being asked for by the client.
.Description
New-PSWebServer creates a web server. The web server is composed of a schema that defines the client's requests to routes where PowerShell code is executed.
Under the covers, New-PSWebServer uses the HTTPListener .NET class to execute powershell code as requested, retrieves the results and sends data back through the httplistener web server framework.