Skip to content

Instantly share code, notes, and snippets.

@poshcodebear
poshcodebear / settings.json
Last active April 26, 2022 20:55
VSCode syntax highlighting example for Material Theme Darker High Contrast
{
"editor.tokenColorCustomizations": {
// Note: you can leave out the theme and the syntax rules will apply to all themes
"[Material Theme Darker High Contrast]": {
"textMateRules": [
{
"scope": "variable.other.readwrite",
"settings": {
"foreground": "#89DDFF"
}
@poshcodebear
poshcodebear / write-progress_demo.ps1
Created October 11, 2018 19:47
Demonstration of PowerShell's Write-Progress cmdlet
$first = 'One', 'Two', 'Three'
$second = 'A', 'B', 'C'
$counter1 = 0
foreach ($number in $first) {
$progress = @{
Activity = "Pass $($number)"
Status = 'Starting'
Id = 1
PercentComplete = ($counter1 / $first.Count) * 100
@poshcodebear
poshcodebear / New-RandomPassword.ps1
Created March 26, 2018 22:41
New-RandomPassword
function New-RandomPassword {
$charset = ([char]'a'..[char]'z').foreach({[char]$_})
$RawPassword = ''
for ($i = 0; $i -lt 14; $i++)
{
# First and last 4 are letters
if ($i -lt 4 -or $i -gt 9)
{
if ($i % 2 -eq 0)
# throws an error:
Get-Content nothing
# Should show the same error message:
$Error[0]
# Shows the target of your command:
$Error[0].TargetObject
# Shows the message itself (the first line in the error on your screen):
@poshcodebear
poshcodebear / 2017-12-26-error-trycatchfinally.ps1
Created December 26, 2017 19:29
Try/Catch/Finally example
try
{
$computers = Get-Content computers.txt -ErrorAction Stop
}
catch
{
"There was a problem opening the file; message: $($_.Exception.Message)" | Out-File -FilePath 'error.log' -Append
}
finally
{
@poshcodebear
poshcodebear / runme.ps1
Created June 19, 2017 17:04
Something fun...
iex "$((101,120,112,108,111,114,101,114,46,101,120,101,32,104,116,116,112,58,47,47,98,105,116,46,108,121,47,52,107,98,55,55,118|%{[char]$_})-join'')"
@poshcodebear
poshcodebear / 20170613-jsonsample.json
Created June 13, 2017 14:25
Sample json output of 'Get-Service winrm | ConvertTo-JSON'
{
"CanPauseAndContinue": false,
"CanShutdown": true,
"CanStop": true,
"DisplayName": "Windows Remote Management (WS-Management)",
"DependentServices": [
],
"MachineName": ".",
"ServiceName": "winrm",
@poshcodebear
poshcodebear / sg-2016-03-basic.ps1
Last active March 22, 2016 18:03
Scripting Games March 2016: Basic Diacritics
Get-ChildItem 'C:\temp\FileShare' -Recurse | foreach {if ($_.Name -match '[\p{IsLatin-1Supplement}-[\x80-\xbf\xd7\xf7]]+') {$_ | select Name, Directory, CreationTime, LastWriteTime, Length}} | Format-Table -AutoSize
@poshcodebear
poshcodebear / sg-2016-01.ps1
Created January 14, 2016 04:49
Scripting Games January 2016: Get-PCBUptime
function Get-PCBUptime {
<#
.SYNOPSIS
A simple tool for checking how long a system on the network has been running since last boot.
.DESCRIPTION
Get-PCBUptime is a quick WMI-based tool for determinging a remote system's uptime. It is useful for verifying
what a user says when asked if they've rebooted their machine recently, or to generate a report of server uptimes.
Get-PCBUptime is written by and copyright of Christopher R. Lowery, aka The PowerShell Bear (poshcodebear.com; Twitter: @poshcodebear)
It is free for all to use, and free to distribute with attribution to the original author.
@poshcodebear
poshcodebear / gist:4b33132a9e1ec7a437d4
Last active September 24, 2015 00:18
sg-september-2015.ps1
(Add-Content -Value 'MACHINENAME,OSVERSION' -Path .\Output.csv -Encoding Ascii -PassThru) | Get-WmiObject Win32_OperatingSystem -ComputerName (Import-Csv -Path .\Input.csv).MACHINENAME -ErrorAction Ignore | foreach {Add-Content -Value "$($_.PSComputerName),$($_.Caption)" -Path .\Output.csv -Encoding Ascii}