Last active
September 5, 2018 15:38
-
-
Save micmaher/3513e91b604a41c4464ea577d5c8c111 to your computer and use it in GitHub Desktop.
Customise the your PowerShell Environment
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 Set-PSEnv{ | |
<# | |
.SYNOPSIS | |
Customise PowerShell Environment | |
.DESCRIPTION | |
Can set up PowerShell profile | |
Can Set up Package Management (PowerShell Gallery), Chocolatey, Implicit Remoting, Themes, | |
Can map a PSDrive to OneDrive, Update Help | |
.NOTES | |
Author: Michael Maher | |
.EXAMPLE | |
Set-PSEnv -MapDrives -Theme Blackboard -Verbose | |
VERBOSE: Profile Already Exists | |
VERBOSE: Creating OneDrive Mapping | |
Name Used (GB) Free (GB) Provider Root | |
---- --------- --------- -------- ---- | |
Library 0.00 437.91 FileSystem C:\Users\micma\OneDrive\Scripts\... | |
VERBOSE: UpdateHelp not requested | |
VERBOSE: PowerShell Gallery not requested | |
VERBOSE: Implicit Remoting not requested | |
VERBOSE: Chocolatey not requested | |
#> | |
[CmdletBinding()] | |
Param | |
( | |
# Map One Drive? | |
[Switch]$MapDrives, | |
# Enable Implicit Remoting? | |
[Switch]$Remoting, | |
# Update PowerShell Help? | |
[Switch]$UpdateHelp, | |
# Set-up PowerShell Gallery | |
[Switch]$PSGallery, | |
# Install Chocolatey | |
[Switch]$Chocolatey, | |
# Install PSVersion | |
[Switch]$PSVersion, | |
# Install Theme | |
[Parameter(mandatory=$false)] | |
[ValidateSet('Ambient', 'Ambients', 'Base16_Tomorrow', 'Blackboard', 'GEdit_Original', | |
'Monokai', 'Navy_Ocean', 'Oblivion', 'Obsidian', 'Ocean_Deep', 'Pastel', 'Placidity', | |
'RecognEyes', 'Retta Roboticket', 'Solarized', 'Solarized_Light', 'Sublime_Text_2', | |
'VS2013_Dark', 'Wombat', 'Zenburn')] | |
[Switch]$Theme) | |
Begin | |
{ | |
# Other Variables | |
$libraryPath='\OneDrive\Scripts\Powershell' | |
$usrProf=$env:USERPROFILE | |
$themesURL = 'https://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e/file/113950/1/ISEColorThemeCmdlets.ps1' | |
} | |
Process | |
{ | |
#region 1. Create a New Profile if None Exists | |
If (-Not(Test-path $profile)){ | |
Write-Verbose "Creating New Profile" | |
New-Item -path $profile -type file –force} | |
Else{ | |
Write-Verbose "Profile Already Exists"} | |
#endregion | |
#region 2. Map One Drive | |
If ($MapDrives){ | |
If (-Not(Test-path OneDrive:)){ | |
Write-Verbose "Creating OneDrive Mapping" | |
New-PSDrive -PSProvider FileSystem -Name OneDrive -Description "PowerShell Script Library on OneDrive"` | |
-Root "$usrProf$libraryPath" -Scope Global | |
Set-Location OneDrive: | |
} | |
Else{ | |
Write-Verbose "Drive Already Exists"} | |
} | |
#endregion | |
#region 3. Update Help | |
If ($UpdateHelp){ | |
# Set Auth to Proxy if required | |
$wc = New-Object System.Net.WebClient | |
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentialstest | |
Update-Help | |
} | |
#endregion | |
#region 4. Set-up PowerShell Gallery/Package Management | |
If ($PSGallery){ | |
Install-PackageProvider -Name NuGet -Force -Verbose | |
Get-PackageProvider -ListAvailable | |
Set-PSRepository -Name psgallery -InstallationPolicy Trusted -Verbose | |
Write-Verbose (Get-PSRepository) | |
#Find-Package -OutVariable p | |
#Write-Verbose "$($p.Count) Packages in the PowerShell Gallery" | |
#Install-Module -Name psscriptanalyzer -Verbose | |
} | |
#endregion | |
#region 5. Set-up Implicit Remoting | |
If ($Remoting){ | |
$DC = Read-Host -Prompt 'Input the DC name for Implict Remoting' | |
$domain = Read-Host -Prompt 'Input the domain name' | |
$username = 'Input the administrative username' | |
$session = New-PSSession -computerName $DC -Credential "$domain\$username" | |
Invoke-command {import-module activedirectory} -session $session | |
Export-PSSession -session $session -commandname *-AD* -outputmodule RemoteAD -allowclobber | |
Import-Module RemoteAD -prefix DC -Verbose | |
# Get-DCADUser -filter "Name -like 'D*'" | |
} | |
#endregion | |
#region 6. Set-up Chocolatey | |
If ($Chocolatey){ | |
Set-Location C: | |
Invoke-Expression ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) -Verbose | |
} | |
#endregion | |
#region 7. Set-up Theme | |
If ($Theme){ | |
Invoke-Expression ((new-object net.webclient).DownloadString($themesURL)) -Verbose | |
Get-ChildItem "$usrProf$libraryPath\Themes\PowerShell_ISE_Themes\$theme" -Include *.ps1xml -recurse | | |
Import-ISEThemeFile -ApplyTheme -ErrorAction SilentlyContinue | |
} | |
#endregion | |
#region 8. Set-up PSVersion | |
If ($PSVersion){ | |
Install-Module -Name PSVersion -Verbose | |
} | |
#endregion | |
} | |
End { | |
Write-Verbose 'Use Set-PSEnv to set other parameters' | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NAME
Set-PSEnv
SYNOPSIS
Customise PowerShell Environment
SYNTAX
Set-PSEnv [-MapDrives] [-Remoting] [-UpdateHelp] [-PSGallery] [-Chocolatey] [-Theme] []
DESCRIPTION
Can set up PowerShell profile
Can Set up Package Management (PowerShell Gallery), Chocolatey, Implicit Remoting, Themes,
Can map a PSDrive to OneDrive, Update Help
PARAMETERS
-MapDrives []
Map One Drive?
INPUTS
OUTPUTS
NOTES