PoshCode Logo PowerShell Code Repository

My Profile.ps1 by Joel Bennett 17 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2096"></script>download | new post

This is my profile script … it contains a few things that are specific to my setup (particularly: which modules I load, and the scripts which I Autoload), but they are all available here on PoshCode.org :-)

  1. ## If your PC doesn't have this set already, someone could tamper with this script...
  2. ## but at least now, they can't tamper with any of the scripts that I auto-load!
  3. Set-ExecutionPolicy AllSigned Process
  4. ## Set the profile directory first, so we can refer to it from now on.
  5. Set-Variable ProfileDir (Split-Path $MyInvocation.MyCommand.Path -Parent) -Option AllScope
  6. ## I determine which modules to pre-load here (in this SIGNED script)
  7. $AutoModules = 'Autoload', 'Strings', 'Authenticode', 'HttpRest', 'PoshCode', 'PowerTab', 'ResolveAliases', 'PSCX'
  8.  
  9. ###################################################################################################
  10. ## Preload all the modules in AutoModules, printing out their names in color based on status
  11. ## No errors while loading modules (I will save them and print them out later)
  12. $ErrorActionPreference = "SilentlyContinue"
  13. Write-Host "Loading Modules: " -Fore Cyan -NoNewLine
  14. $AutoRunErrors = @()
  15. ForEach( $module in $AutoModules ) {
  16.    Import-Module $module -EA SilentlyContinue -EV +script:AutoRunErrors
  17.    if($?) {  
  18.       Write-Host "$module " -fore Cyan -NoNewLine  
  19.    } else {
  20.       Write-Host "$module " -fore Red -NoNewLine
  21.    }
  22. }
  23. ###################################################################################################
  24.  
  25. Write-Host
  26. $ErrorActionPreference = "Continue"
  27. # Write out the error messages if we missed loading any modules
  28. if($AutoRunErrors) { $AutoRunErrors | Out-String | Write-Host -Fore Red }
  29.  
  30. ###################################################################################################
  31. ## I have a few additional custom type and format data items which take prescedence over anyone else's
  32. Update-TypeData   -PrependPath "$ProfileDir\Formats\Types.ps1xml"
  33. Update-FormatData -PrependPath "$ProfileDir\Formats\Formats.ps1xml"
  34.  
  35. ###################################################################################################
  36. ## Developer tools stuff ...
  37. ## I need InstallUtil, MSBuild, and TF (TFS) and they're all in the.Net RuntimeDirectory OR Visual Studio*\Common7\IDE
  38. $Env:Path = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() + ";" +
  39.             @(Get-Item C:\Program*Files*\*Visual?Studio*\Common7\IDE\TF.exe -EA 0| Sort LastWriteTime -Desc)[0].Directory +
  40.             ";$Env:Path"
  41.  
  42. if(!(Get-Command TF.exe -ErrorAction SilentlyContinue)) {
  43.         Write-Warning "Could not locate TF.exe in Visual Studio\Common7. It may not be installed on this PC"
  44. }      
  45.  
  46. ###################################################################################################
  47. ## And a couple of functions that can't be saved as script files, and aren't worth modularizing
  48. ## Elipsis shortcut for Select -Expand name
  49. ${function:...} = { process { $_.$($args[0]) } }
  50.  
  51. ## The Trinary shortcut function
  52. function ?: {
  53.    param([ScriptBlock]$trueblock,[ScriptBlock]$falseblock)
  54.    process {  
  55.       if($_){
  56.          &$trueblock $_
  57.       } else {
  58.          &$falseblock $_
  59.       }
  60.    }
  61. }
  62.  
  63. ## Pre-load a few of my functions in case I need them.
  64. ## In particular, a couple that are better than PSCX's versions
  65. Autoload New-ShortCut
  66. Autoload Edit-File
  67. Autoload Get-PerformanceHistory
  68.  
  69. ###################################################################################################
  70. ## Custom aliases I can't live without
  71. Set-Alias   exec         Invoke-Expression         -Option AllScope -Description "Personal Function alias"
  72. Set-Alias   edit         Edit-File                 -Option AllScope -Description "Personal Function alias"
  73. Set-Alias   rand         Get-Random                -Option AllScope -Description "Personal Cmdlet Alias"
  74. Set-Alias   say          Out-Speech                -Option AllScope -Description "Personal Script Alias"
  75. Set-Alias   gph          Get-PerformanceHistory    -Option AllScope -Description "Personal Script Alias"
  76.  
  77. ###################################################################################################
  78. ## I love my random quotes ...
  79. Set-Variable QuoteDir (Resolve-Path (Join-Path (Split-Path $ProfileDir -parent) "@stuff\Quotes")) -Scope Global -Option AllScope -Description "Personal PATH Variable"
  80.  
  81. Set-Alias   RandomLine   Select-RandomLine         -Option AllScope -Description "Personal Script Alias"
  82. Set-Alias   Get-Quote    Select-RandomLine         -Option AllScope -Description "Personal Script Alias"
  83. Set-Alias   gq           Select-RandomLine         -Option AllScope -Description "Personal Script Alias"
  84.  
  85. ###################################################################################################
  86. ## I add my "Scripts" directory and all of its direct subfolders to my PATH
  87. $ENV:PATH = Get-ChildItem $ProfileDir\Script[s],$ProfileDir\Scripts\* |
  88.                Where-Object { $_.PsIsContainer } |
  89.                ForEach-Object { $_.FullName } |
  90.                Join ";" -append $ENV:PATH -unique
  91.  
  92. ## I like all of my sessions to start in my profile directory
  93. Set-Location $ProfileDir
  94. ## My prompt function is in it's own script
  95. . Set-Prompt
  96.  
  97. ## And finally, relax the code signing restriction so we can actually get work done
  98. Set-ExecutionPolicy RemoteSigned Process
  99.  
  100. ## Get a random quote, and print it in yellow :D
  101. Write-Host $(Get-Quote) -Fore Yellow
  102.  
  103. # SIG # Begin signature block
  104. # MIIIDQYJKoZIhvcNAQcCoIIH/jCCB/oCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
  105. # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
  106. # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUTf/G2RuXibbNw/GxUxoBclEt
  107. # kOKgggUrMIIFJzCCBA+gAwIBAgIQKQm90jYWUDdv7EgFkuELajANBgkqhkiG9w0B
  108. # AQUFADCBlTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAlVUMRcwFQYDVQQHEw5TYWx0
  109. # IExha2UgQ2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBOZXR3b3JrMSEwHwYD
  110. # VQQLExhodHRwOi8vd3d3LnVzZXJ0cnVzdC5jb20xHTAbBgNVBAMTFFVUTi1VU0VS
  111. # Rmlyc3QtT2JqZWN0MB4XDTEwMDUxNDAwMDAwMFoXDTExMDUxNDIzNTk1OVowgZUx
  112. # CzAJBgNVBAYTAlVTMQ4wDAYDVQQRDAUwNjg1MDEUMBIGA1UECAwLQ29ubmVjdGlj
  113. # dXQxEDAOBgNVBAcMB05vcndhbGsxFjAUBgNVBAkMDTQ1IEdsb3ZlciBBdmUxGjAY
  114. # BgNVBAoMEVhlcm94IENvcnBvcmF0aW9uMRowGAYDVQQDDBFYZXJveCBDb3Jwb3Jh
  115. # dGlvbjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMfUdxwiuWDb8zId
  116. # KuMg/jw0HndEcIsP5Mebw56t3+Rb5g4QGMBoa8a/N8EKbj3BnBQDJiY5Z2DGjf1P
  117. # n27g2shrDaNT1MygjYfLDntYzNKMJk4EjbBOlR5QBXPM0ODJDROg53yHcvVaXSMl
  118. # 498SBhXVSzPmgprBJ8FDL00o1IIAAhYUN3vNCKPBXsPETsKtnezfzBg7lOjzmljC
  119. # mEOoBGT1g2NrYTq3XqNo8UbbDR8KYq5G101Vl0jZEnLGdQFyh8EWpeEeksv7V+YD
  120. # /i/iXMSG8HiHY7vl+x8mtBCf0MYxd8u1IWif0kGgkaJeTCVwh1isMrjiUnpWX2NX
  121. # +3PeTmsCAwEAAaOCAW8wggFrMB8GA1UdIwQYMBaAFNrtZHQUnBQ8q92Zqb1bKE2L
  122. # PMnYMB0GA1UdDgQWBBTK0OAaUIi5wvnE8JonXlTXKWENvTAOBgNVHQ8BAf8EBAMC
  123. # B4AwDAYDVR0TAQH/BAIwADATBgNVHSUEDDAKBggrBgEFBQcDAzARBglghkgBhvhC
  124. # AQEEBAMCBBAwRgYDVR0gBD8wPTA7BgwrBgEEAbIxAQIBAwIwKzApBggrBgEFBQcC
  125. # ARYdaHR0cHM6Ly9zZWN1cmUuY29tb2RvLm5ldC9DUFMwQgYDVR0fBDswOTA3oDWg
  126. # M4YxaHR0cDovL2NybC51c2VydHJ1c3QuY29tL1VUTi1VU0VSRmlyc3QtT2JqZWN0
  127. # LmNybDA0BggrBgEFBQcBAQQoMCYwJAYIKwYBBQUHMAGGGGh0dHA6Ly9vY3NwLmNv
  128. # bW9kb2NhLmNvbTAhBgNVHREEGjAYgRZKb2VsLkJlbm5ldHRAWGVyb3guY29tMA0G
  129. # CSqGSIb3DQEBBQUAA4IBAQAEss8yuj+rZvx2UFAgkz/DueB8gwqUTzFbw2prxqee
  130. # zdCEbnrsGQMNdPMJ6v9g36MRdvAOXqAYnf1RdjNp5L4NlUvEZkcvQUTF90Gh7OA4
  131. # rC4+BjH8BA++qTfg8fgNx0T+MnQuWrMcoLR5ttJaWOGpcppcptdWwMNJ0X6R2WY7
  132. # bBPwa/CdV0CIGRRjtASbGQEadlWoc1wOfR+d3rENDg5FPTAIdeRVIeA6a1ZYDCYb
  133. # 32UxoNGArb70TCpV/mTWeJhZmrPFoJvT+Lx8ttp1bH2/nq6BDAIvu0VGgKGxN4bA
  134. # T3WE6MuMS2fTc1F8PCGO3DAeA9Onks3Ufuy16RhHqeNcMYICTDCCAkgCAQEwgaow
  135. # gZUxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtl
  136. # IENpdHkxHjAcBgNVBAoTFVRoZSBVU0VSVFJVU1QgTmV0d29yazEhMB8GA1UECxMY
  137. # aHR0cDovL3d3dy51c2VydHJ1c3QuY29tMR0wGwYDVQQDExRVVE4tVVNFUkZpcnN0
  138. # LU9iamVjdAIQKQm90jYWUDdv7EgFkuELajAJBgUrDgMCGgUAoHgwGAYKKwYBBAGC
  139. # NwIBDDEKMAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgor
  140. # BgEEAYI3AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQURIrjK39r
  141. # ycFCQgjjCok8yydU410wDQYJKoZIhvcNAQEBBQAEggEAaDce6yCDu4TU2JQzCji+
  142. # JlREUjrc5achsjJXN3KqaElxEn4WDX2SZzSy+AfgthUkt0g+usfQhLDmqNBaXJnw
  143. # vlrvlq64ABY2z/fBUv5KurXOiSvDxWSGJTflN960TQsKdaO1dzisOSUxizUGAold
  144. # r4JvfnsRXGRTFSGnlOvi54VhOlypgsZuh0BXL2JF/6huGWNKXL1eXKz/gFArIeHB
  145. # +EcOenddxaUtlhmVV9s3As/MQS1/NRfFy5qa6gmG0nj9Vpn+2nzwMeMiXE4dAiJc
  146. # nQ09/WOFXrjhZZIEyNwwdl6lVoJTsL1XTIFOiU2UFK9V8xhNSjIqaGMj9uK1KP5u
  147. # zQ==
  148. # SIG # End signature block

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me