This is a collection of Powershell snippets that I'll add to when I find something useful.
Last active
September 3, 2016 04:02
-
-
Save bgshacklett/6ecf01b5c617550b9177 to your computer and use it in GitHub Desktop.
Powershell Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get all of the OUs in an existing domain and export them to cli-xml | |
# The path is added to each OU object so that they will be placed properly | |
# by the New-ADOrganizationalUnit command | |
Get-ADOrganizationalUnit -Filter * -Properties * | | |
select *, @{Name="Path";Expression={$_.DistinguishedName -replace "^.*?\,",""}} | | |
Export-Clixml .\prodOUs.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-EC2Instance | Select-Object -ExpandProperty instances | Select-Object InstanceId, @{Name='Name'; Expression={$_.Tags | Where-Object { $_.Key -like 'Name'} | Select-Object -ExpandProperty Value }}, InstanceType, @{Name='AMI'; Expression={Get-EC2Image -ImageId $_.ImageID | Select-Object -ExpandProperty name}}, @{Name='StorageConfig'; Expression={"Storage"}}, @{Name='SubnetName'; Expression={Get-Ec2Subnet -SubnetId $_.SubnetId | Select-Object -ExpandProperty Tags | Where-Object { $_.Key -like 'Name' } | Select-Object -ExpandProperty Value }} | Sort-Object -Property Name | Out-GridView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter Get-FilteredParameter | |
{ | |
param | |
( | |
$ExcludedParameters, | |
[Switch]$ExcludeCommonParameters, | |
[Switch]$ExcludeOptionalCommonParameters | |
) | |
begin | |
{ | |
$commonParameters = | |
[System.Management.Automation.Cmdlet]::CommonParameters | |
$optionalCommonParameters = | |
[System.Management.Automation.Cmdlet]::OptionalCommonParameters | |
$filteredParameters = $() | |
$filteredParameters += $ExcludedParameters | |
If ($ExcludeCommonParameters) | |
{ | |
$filteredParameters += $commonParameters | |
} | |
If ($ExcludeOptionalCommonParameters) | |
{ | |
$filteredParameters += $optionalCommonParameters | |
} | |
} | |
process | |
{ | |
If ($filteredParameters -notcontains $_.Key) { $_ } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-FreeSpace | |
{ | |
param ( $ComputerName='localhost' ) | |
$wmiParams = | |
@{ | |
'Class' = 'Win32_LogicalDisk'; | |
'ComputerName' = $ComputerName; | |
} | |
$sizeProperty = | |
@{ | |
'Name' = 'GB Free'; | |
'Expression' = {($_.FreeSpace / 1Gb).ToString("#.##")} | |
} | |
Get-WmiObject @wmiParams | select Name,Size,$sizeProperty | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-LocalUser ($Computername = $env:COMPUTERNAME) { | |
$params = | |
@{ | |
'Class' = 'Win32_UserAccount'; | |
'Filter' = 'LocalAccount=True'; | |
'ComputerName' = $ComputerName; | |
} | |
Get-WmiObject @params | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-AWSRegion ` | |
| ForEach-Object ` | |
{ | |
$_.Region; | |
$noDefaultFilter = [Amazon.EC2.Model.Filter]@{Name = 'isDefault'; Values = 'false' } | |
Get-EC2Vpc -Region $_.Region -Filter $noDefaultFilter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# After backing up GPOs with Backup-GPO -All -Path $destPath | |
# the GPOs can be imported to another domain with the following | |
# snippet | |
cd $pathToBackedUpGPOs | |
ls | | |
% { | |
$gpoName = $(gc "$_\bkupInfo.xml" | | |
Select-Xml "//*[name()='GPODisplayName'] " | | |
% {$_.Node.InnerText}) | |
$params = | |
@{ | |
'BackupId'=$_.Name; | |
'Path'=$PWD; | |
'TargetName'=$gpoName; | |
'CreateIfNeeded'=$True; | |
} | |
Import-GPO @params | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment