Skip to content

Instantly share code, notes, and snippets.

The PowerShell Bear poshcodebear

Block or report user

Report or block poshcodebear

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@poshcodebear
poshcodebear / write-progress_demo.ps1
Created Oct 11, 2018
Demonstration of PowerShell's Write-Progress cmdlet
View write-progress_demo.ps1
$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
View New-RandomPassword.ps1
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)
View 2017-12-26-error-examples.ps1
# 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):
View 2017-12-26-error-trycatchfinally.ps1
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
{
View runme.ps1
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 Jun 13, 2017
Sample json output of 'Get-Service winrm | ConvertTo-JSON'
View 20170613-jsonsample.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 Mar 22, 2016
Scripting Games March 2016: Basic Diacritics
View sg-2016-03-basic.ps1
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 Jan 14, 2016
Scripting Games January 2016: Get-PCBUptime
View sg-2016-01.ps1
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.
View gist:4b33132a9e1ec7a437d4
(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}
@poshcodebear
poshcodebear / sg-august.2015.ps1
Last active Aug 29, 2015
Scripting Games August 2015 entry
View sg-august.2015.ps1
# Works with PowerShell v2, but only works with very simple JSON
function Get-SimpleJSON {
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$True)]
[Alias('Uri')]
[string]$JSONUri,
[Alias('CamelCase')]
[switch]$TitleCase
You can’t perform that action at this time.