Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
mattmcnabb / Test-ConditionalParameter.ps1
Last active August 29, 2015 14:19
Powershell Conditional Dynamic Parameters
function Test-ConditionalParameter
{
param
(
[validateset('Value1','Value2')]
[parameter()]
$Parameter1
)
dynamicparam
@mattmcnabb
mattmcnabb / Test-BeginBlock
Created May 1, 2015 22:48
Begin Block Variable
function Test-BeginBlock
{
[CmdletBinding()]
param
(
[parameter(ValueFromPipeline)]
$Parameter1
)
begin { $Variable1 = 'whatev' }
@mattmcnabb
mattmcnabb / Set-WorkingDirectory.ps1
Last active August 29, 2015 14:20
Powershell ISE: Automatically change working directory
$null = Register-ObjectEvent -InputObject $psISE.PowerShellTabs.SelectedPowerShellTab -EventName 'PropertyChanged' -Action {
if($args[1].PropertyName -eq 'LastEditorWithFocus' -and $env:AutoChangeLocation -eq $true)
{
$Location = Get-Location
$NewLocation = split-path $psISE.PowerShellTabs.SelectedPowerShellTab.Files.SelectedFile.FullPath
if ($Location.path -ne $NewLocation)
{
Set-Location $NewLocation
Out-Host -InputObject ' '
prompt
@mattmcnabb
mattmcnabb / IndentStyle.ps1
Last active August 29, 2015 14:21
IndentStyle
# Indent style for multiline hashtables
# I tend to think of the assignment and opening '@{' as
# one token.
$Capabilities = @{
MilitaryService = $false
DrinkAlcohol = $false
Vote = $false
}
# This usage works nearly everywhere for me
@mattmcnabb
mattmcnabb / ConnectExchange.ps1
Last active August 29, 2015 14:21
Use Powershell to Manage Office 365
$Credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session
@mattmcnabb
mattmcnabb / ConnectO365Other.ps1
Created May 18, 2015 00:40
Connect to Sharepoint, Lync, and Azure Active Directory
# Connect to Azure Active Directory to manage users, groups and licensing
Connect-MsolService -Credential $Credential
# Connect to Sharepoint Online for domain psescape.onmicrosoft.com
Connect-SpoService -Credential $Credential -Url https://psescape-admin.sharepoint.com
# Connect to Lync Online
$Session = New-CsOnlineSession -Credential $Credential
Import-PSSession $Session
@mattmcnabb
mattmcnabb / Connect-O365.ps1
Created May 18, 2015 00:51
Connect to Office 365 Services Using the O365 Admin Module
Connect-O365 -Services AzureActiveDirectory, Exchange, Sharepoint, Skype -Credential $Credential
@mattmcnabb
mattmcnabb / Test-Star.ps1
Created May 21, 2015 14:43
Adding a Parameter that supports '*'
function Test-Star
{
[CmdletBinding()]
param
(
[ValidateSet('Option1','Option2','Option3','*')]
[ValidateScript({ ($_ -contains '*' -and $_ -isnot [array]) -or ($_ -notcontains '*') })]
$StarParam
)
@mattmcnabb
mattmcnabb / EnumBug.ps1
Last active August 29, 2015 14:22
Is this a bug? What's the deal?
# Add an enum with All constant
Add-Type -TypeDefinition @'
using System;
[Flags]
public enum O365Services
{
AzureActiveDirectory,
Exchange,
Sharepoint,
@mattmcnabb
mattmcnabb / 2015JulySG.ps1
Last active August 29, 2015 14:24
2015-July Scripting Games
gwmi CIM_operatingsystem|Select PSC*,*j*,v*,@{n='BIOSSerial';e={$_.SerialNumber}}