Skip to content

Instantly share code, notes, and snippets.

@poshcodebear
poshcodebear / discover-severed-inheritance.ps1
Created July 31, 2015 06:07
Discovering folders with severed inheritance
$Server = 'server'
$Shares = Get-Content -Path C:\temp\server-shares.txt
$output = foreach ($Share in $Shares) {
$Path = "\\$Server\$Share"
Get-ChildItem -Path $Path -Recurse | `
where attributes -like *directory* | `
Get-Acl | `
Where-Object {$_.AreAccessRulesProtected -eq $True} | `
Select-Object @{Label='Directory';Expression={$_.path.split(':')[-1]}}
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.
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 / test-speed-measure-command.ps1
Created July 31, 2015 06:17
Test execution speed with Measure-Command
$computers = Get-ADComputer -SearchBase (Get-ADOrganizationalUnit -Filter {Name -eq 'Servers'} ) -Filter * | Select-Object -ExpandProperty Name
Measure-Command {
Get-WmiObject Win32_ComputerSystem -ComputerName $computers -ErrorAction ignore
}
Measure-Command {
Get-CimInstance Win32_ComputerSystem -ComputerName $computers -ErrorAction ignore
}
@poshcodebear
poshcodebear / measure-command-sample-output.txt
Created July 31, 2015 06:19
Test execution speed with Measure-Command - Sample output
# Get-WMIObject:
Days : 0
Hours : 0
Minutes : 7
Seconds : 11
Milliseconds : 568
Ticks : 4315681996
TotalDays : 0.00499500231018519
TotalHours : 0.119880055444444
TotalMinutes : 7.19280332666667
@poshcodebear
poshcodebear / sg-august.2015.ps1
Last active August 29, 2015 14:26
Scripting Games August 2015 entry
# 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
@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}
@poshcodebear
poshcodebear / protect-all-ous.ps1
Last active October 4, 2015 05:32
Protecting all OUs from accidental deletion
Get-ADOrganizationalUnit -Filter * -Properties ProtectedFromAccidentalDeletion | `
where {$_.ProtectedFromAccidentalDeletion -eq $False} | `
Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $True
@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 / 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 / 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",