Skip to content

Instantly share code, notes, and snippets.

@micmaher
Last active September 5, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save micmaher/3513e91b604a41c4464ea577d5c8c111 to your computer and use it in GitHub Desktop.
Save micmaher/3513e91b604a41c4464ea577d5c8c111 to your computer and use it in GitHub Desktop.
Customise the your PowerShell Environment
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'
}
}
@micmaher
Copy link
Author

micmaher commented Apr 23, 2016

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?

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Remoting [<SwitchParameter>]
    Enable Implicit Remoting?

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

-UpdateHelp [<SwitchParameter>]
    Update PowerShell Help?

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

-PSGallery [<SwitchParameter>]
    Set-up PowerShell Gallery

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Chocolatey [<SwitchParameter>]
    Install Chocolatey

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Theme [<SwitchParameter>]
    Install Theme

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

<CommonParameters>
    This cmdlet supports the common parameters: Verbose, Debug,
    ErrorAction, ErrorVariable, WarningAction, WarningVariable,
    OutBuffer, PipelineVariable, and OutVariable. For more information, see 
    about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 

INPUTS

OUTPUTS

NOTES

    Author: Michael Maher

-------------------------- EXAMPLE 1 --------------------------

PS C:\>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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment