Sunday, 20 January 2013

Open-FunctionInISE

  1. Function Open-FunctioninISE {
  2. <#
  3. .SYNOPSIS
  4. Opens a function in ISE
  5. .DESCRIPTION
  6.     This enables you to specify the function to open. The definition
  7.     comes from (Get-Command <command>).definition.
  8.     You specify the name of the function to open, or select it
  9.     in the ISE
  10. .NOTES
  11.     File Name : Open-FunctionInIse.ps1
  12.     Author : Thomas Lee - tfl@psp.co.uk
  13.     Requires : PowerShell Version 3.0
  14.     Based on a technet script published at http://gallery.technet.microsoft.com/scriptcenter/Open-defined-functions-in-22788d0f 
  15. .LINK
  16.     This script posted to:
  17.         http://www.pshscripts.blogspot.com
  18. .PARAMETER Function
  19.     The name of a defined function to open in new PowerShell ISE tab.
  20. .EXAMPLE
  21.     C:\Psh> Open-FunctionInIse Open-FunctioninISE
  22.     Opens Open-FunctionInIse in a new PowerShell ISE tab
  23. .EXAMPLE
  24.     Select a function name in an open edit window, then hit
  25.     Ctrl+Shift+E (or use the Add-ons menu)
  26. #>
  27. # Define parameters
  28. [Cmdletbinding()]
  29. Param(
  30. [Parameter(Position=0)]
  31. [ValidateScript({ Get-Command -commandtype function -name $_ })]
  32. [String] $function
  33. )
  34. # Start of the function.
  35. Process{
  36. # Get the function name (i.e. the selected text) and ensure it's not empty
  37. $fn = $psise.currentfile.Editor.selectedText
  38. # if nothing selected, see if we got called with it
  39. If (!$fn -or ($fn.length -LE 0)) {
  40. if ($function -and ($function.length -GT 0)){
  41. $fn = $function}
  42. Else {
  43. $fn = Read-Host -Prompt "Enter function name to view"
  44. }
  45. }
  46. If (!$Fn) {'No function to edit';return}
  47. # Get the definition, if there is one
  48. $definition = (Get-Command -commandtype function -name $fn).definition
  49. If (!$definition -or ($Definition.Length -le 0)) {return "Function [$fn] not found"}
  50. # Create What to see
  51. $FunctionHeader = "Function $fn {`n"
  52. $comments = "`# Description : $($definition.description)`n"
  53. $comments += "`# Module : $($definition.module)`n`n"
  54. $FunctionTrailer = "`n}"
  55. # Wrap it all up
  56. $definition = $functionHeader + $Comments + $Definition + $FunctionTrailer
  57. "function $fn {" + $definition + "}"
  58. # Add a tab, add text to the tab, set caret position to first character
  59. $tab = $Psise.CurrentPowerShellTab.Files.Add()
  60. $tab.Editor.text = $definition
  61. $tab.Editor.SetCaretPosition(1,1)
  62. # Sleep for a moment. Ran into issues without this.
  63. Start-Sleep -Milliseconds 200
  64. } # End process block
  65. } # End Function
  66. Set-ALias OFISE .\Open-FunctionInISE
  67. # Here we could test the function from the command line.
  68. # Function test123 {'testing 1-2-3'}
  69. # Open-FunctioninISE test123
  70. # I assume you'll just use this function so I've left the following
  71. # Code in place to add this as a menu item
  72. # Here add to the ISE as long as it's not there already!
  73. $x = ($psise.CurrentPowerShellTab.AddOnsMenu.Submenus).displayname
  74. if (! ($x -contains "_Edit Function in ISE")) {
  75. $Psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("_Edit Function in ISE", {Open-FunctioninISE},
  76. "Ctrl+Shift+E") | Out-Null
  77. }
  78. Else {
  79. 'Function already added in ISE'
  80. }

Tuesday, 15 January 2013

Create-ReferenceVHDX.ps1

  1. # Create-ReferenceVHDX.ps1
  2. # Script that will create a reference VHDX for later VM Creation
  3. # Version 1.0.0 - 14 Jan 2013
  4. # Define a function to create a reference VHDX.
  5. Function Create-ReferenceVHDX {
  6. [Cmdletbinding()]
  7. Param (
  8. # ISO of OS
  9. [string] $Iso = 'C:\downloads\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso',
  10. # Path to reference VHD
  11. [string] $RefVHDXPath = "C:\vhd\Ref2012.vhdx"
  12. )
  13. # Get start time
  14. $StartTime = Get-Date
  15. Write-Verbose "Beginning at $StartTime"
  16. #--------------------------------------------------+
  17. # Mount an ISO and check out available OS versions!
  18. #--------------------------------------------------+
  19. # Import the DISM module
  20. Write-Verbose 'Loading DISM module' -Verbose:$false
  21. Import-Module -Name DISM -Verbose:$False
  22. # Mount the OS ISO image onto the local machine
  23. Write-Verbose "Mounting ISO image [$iso]"
  24. Mount-DiskImage -ImagePath $iso
  25. # Get the Volume the Image is mounted to
  26. Write-Verbose 'Getting disk image of the ISO'
  27. $ISOImage = Get-DiskImage -ImagePath $ISO | Get-Volume
  28. Write-Verbose "Got disk image [$($ISOImage.DriveLetter)]"
  29. # And get the drive Letter of the dirve where the image is mounted
  30. # add the drive letter separator (:)
  31. $ISODrive = [string]$ISOImage.DriveLetter+":"
  32. Write-Verbose "OS ISO mounted on drive letter [$ISODrive]"
  33. # Next we will get the installation versions from the install.wim.
  34. # $Indexlist is the index of WIMs on the DVD - display the versions
  35. # available in the DVD and let user select the one to serve as the base
  36. # image - probably DataCentre Full Install
  37. $IndexList = Get-WindowsImage -ImagePath $ISODrive\sources\install.wim
  38. Write-Verbose 'Got Index list - displaying it now!'
  39. # Display the list and return the index
  40. $item = $IndexList | Out-GridView -OutputMode Single
  41. $index = $item.ImageIndex
  42. Write-Verbose "Selected image index [$index]"
  43. #---------------------------------+
  44. # Create a Reference Image !
  45. #---------------------------------+
  46. # Create the VHDX for the reference image
  47. $VMDisk01 = New-VHD –Path $RefVHDXPath -SizeBytes 15GB
  48. Write-Verbose "Created VHDX File [$($vmdisk01.path)]"
  49. # Get the disk number
  50. Mount-DiskImage -ImagePath $RefVHDXPath
  51. $VHDDisk = Get-DiskImage -ImagePath $RefVHDXPath | Get-Disk
  52. $VHDDiskNumber = [string]$VHDDisk.Number
  53. Write-Verbose "IReference image is on disk number [$VhddiskNumber]"
  54. # Create a New Partition
  55. Initialize-Disk -Number $VHDDiskNumber -PartitionStyle MBR
  56. $VHDDrive = New-Partition -DiskNumber $VHDDiskNumber -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -Confirm:$false
  57. $VHDVolume = [string]$VHDDrive.DriveLetter+":"
  58. Write-Verbose "VHD drive [$vhddrive], Vhd volume [$vhdvolume]"
  59. # Execute DISM to apply image to base disk
  60. Write-Verbose 'Using DISM to apply image to the volume'
  61. Write-Verbose 'This will take some time'
  62. Dism.exe /apply-Image /ImageFile:$ISODrive\Sources\install.wim /index:$Index /ApplyDir:$VHDVolume\
  63. # Execute BCDBoot so volume will boot
  64. Write-Verbose 'Setting BCDBoot'
  65. BCDBoot.exe $VHDVolume\Windows /s $VHDVolume /f BIOS
  66. # Dismount the Images
  67. Write-Verbose "Dismounting ISO and new disk"
  68. Dismount-DiskImage -ImagePath $ISO
  69. Dismount-DiskImage -ImagePath $RefVHDXPath
  70. Write-Verbose "Created Reference Disk [$RefVHDXPath]"
  71. Get-ChildItem $RefVHDXPath
  72. $FinishTime = Get-Date
  73. $tt= $FinishTime - $StartTime
  74. Write-Verbose "Finishing at $FinishTime"
  75. Write-verbose "Creating base image took [$($tt.totalminutes)] minutes"
  76. } # End of Create-ReferenceVHDX
  77. ################################################################################################################
  78. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  79. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  80. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  81. # Path to Server 2012 DVD
  82. $iso = 'C:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso'
  83. # PathTo the reference VDHX is to go
  84. $refvhdxpath = 'C:\V3\ref2012.vhdx'
  85. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  86. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  87. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  88. #################################################################################################################
  89. Create-ReferenceVHDX -iso $iso -RefVHDXPath $RefVHDXPath -Verbose

Saturday, 22 December 2012

Show-Exceptions.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets all the exceptions you can trap by PowerShell 
  4. .DESCRIPTION 
  5.     This script looks at all the loaded assemblies to get all 
  6.     the exceptions you can trap/catch using PowerShell. The 
  7.     display only covers those parts of the .NET framework are loaded. 
  8. .NOTES 
  9.     File Name  : Show-Exceptions.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Psh> .\Show-Exceptions.ps1 -Summary 
  17.     In 69 loaded assemblies, you have 418 exceptions: 
  18.  
  19. .EXAMPLE 
  20.     Psh> .\Show-Exceptions.ps1 
  21.     In 69 loaded assemblies, you have 418 exceptions: 
  22.  
  23.     Name                     FullName                                                 
  24.     ----                     --------                                                 
  25.     _Exception               System.Runtime.InteropServices._Exception                
  26.     AbandonedMutexException  System.Threading.AbandonedMutexException                 
  27.     AccessViolationException System.AccessViolationException                      
  28.     ... 
  29.      
  30. #> 
  31.  
  32. [CMDLETBINDING()] 
  33. Param ( 
  34. [switch] $summary 
  35. ) 
  36. #    Get all the exceptions 
  37. $assemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() 
  38. $exceptions = $Assemblies | ForEach { 
  39.      $_.GetTypes() | where { $_.FullName -Match "System$filter.*Exception$" } } 
  40.  
  41. # Now display the numbers checking for summary flag 
  42. "In {0} loaded assemblies, you have {1} exceptions:" -f $assemblies.count, $exceptions.count 
  43. If (-not $summary) { 
  44. $Exceptions | sort name | format-table name, fullname 
  45. } 
Technorati Tags:

Sunday, 9 December 2012

Get-LyncAutoLogonStatus.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reports on whether Lync should 
  4.     automatically start on a machine when a user 
  5.     logs in. 
  6. .DESCRIPTION 
  7.     This script looks in the registry at a chosen machine 
  8.     to determine if the Lync client should automatically 
  9.     attempt to login when a user logs onto that system. 
  10. .NOTES 
  11.     File Name  : Get-LyncAutoLogonStatus 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14.                  Lync 2010 or later 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     Psh> .\Get-LyncAutoLogonStatus     
  20.     Automatically start Lync when I log on to Windows: True 
  21. #> 
  22.  
  23.  
  24. [Cmdletbinding()] 
  25. Param ( 
  26. [string] $computer = "Cookham8.Cookham.net") 
  27.  
  28.  
  29. # Get the relevant registry key 
  30. $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("CurrentUser", $computer) 
  31. $key = $registry.OpenSubKey("SOFTWARE\Microsoft\Communicator", $True) 
  32.   
  33. # now write to host the details 
  34. Write-Host "Automatically start Lync when I log on to Windows:",` 
  35.     ([boolean] $key.GetValue("AutoRunWhenLogonToWindows",$null)) 
Technorati Tags: ,

Sunday, 2 December 2012

Set-AdminLogon.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that sets autologon 
  4. .DESCRIPTION 
  5.     Autologon enables the system to logon after a reboot without 
  6.     you needing to enter credentials. This is an ideal scenario 
  7.     for lab or training room systems. This script defines 
  8.     a function that sets a userid/password and autologon.
  9. .NOTES 
  10.     File Name  : Set-AdminLogon 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.     Psh> New-AdminLogon -user cookham\tfl -password 'JerryGarciaR0cks!' 
  18.     Auto logon created for [Cookham\tfl] with password: [JerryGarciaR0cks]          
  19.  
  20. #> 
  21.  
  22. Function New-AdminLogon { 
  23.  
  24. [cmdletbinding()] 
  25. Param( 
  26. [string] $User     = $(Throw 'No user id specified'), 
  27. [string] $Password = $(Throw 'No password specified') 
  28. ) 
  29.  
  30. # Define registry path for autologon 
  31. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  32.  
  33. # Set autologon 
  34. Set-ItemProperty -Path $RegPath -Name AutoAdminLogon -Value 1  
  35.  
  36. # Set userid and password 
  37. Set-ItemProperty -Path $RegPath -Name DefaultUserName -Value $User  
  38. Set-ItemProperty -Path $regPath -Name DefaultPassword -Value $Password  
  39.  
  40. # Say nice things and exit! 
  41. Write-Host ("Auto logon [{0}] set to password: [{1}]" -f $user, $password) 
  42. } 
  43.  
  44. Set-AdminLogon -User 'Cookham\tfl' -Password 'JerryGarciaR0cks' 
Technorati Tags: ,

Remove-AdminLogon.ps1

  1. <#
  2. .SYNOPSIS
  3. This script defines a function that removes autologon
  4. .DESCRIPTION
  5. Autologon enables the system to logon after a reboot without
  6. you needing to enter credentials. This is an ideal scenario
  7. for lab or training room systems. This script defines
  8. a function that removes the autologon registry keys
  9. .NOTES
  10. File Name : Remove-AdminLogon
  11. Author : Thomas Lee - tfl@psp.co.uk
  12. Requires : PowerShell Version 2.0
  13. .LINK
  14. This script posted to:
  15. http://www.pshscripts.blogspot.com
  16. .EXAMPLE
  17. Psh> Remove-AdminLogon
  18. Auto logon settings removed
  19. #>
  20. Function Remove-AdminLogon {
  21. [Cmdletbinding()]
  22. Param()
  23. # Define registry path for autologon
  24. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
  25. # Remove autologon property
  26. Remove-ItemProperty -Path $RegPath -Name AutoAdminLogon
  27. # Remove userid and password
  28. Remove-ItemProperty -Path $RegPath -Name DefaultUserName
  29. Remove-ItemProperty -Path $RegPath -Name DefaultPassword
  30. # Say nice things and exit!
  31. Write-Host ("Auto logon removed")
  32. }
  33. Remove-AdminLogon
Technorati Tags: ,

New-AutoAdminLogon.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that sets autologon 
  4. .DESCRIPTION 
  5.     Autologon enables the system to logon after a reboot without 
  6.     you needing to enter credentials. This is an ideal scenario 
  7.     for lab or training room systems. This script defines 
  8.     a function that sets a userid/password and autologon. It does NOT 
  9.     check to see if the value entries already exist. 
  10. .NOTES 
  11.     File Name  : Set-AutoAdminLogon 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17. .EXAMPLE 
  18.     Psh> Set-AutoAdminLogon -user cookham\tfl -password 'JerryGarciaR0cks!' 
  19.     Auto logon set for [Cookham\tfl] with password: [JerryGarciaR0cks]          
  20.  
  21. #> 
  22.  
  23. Function Set-AdminLogon { 
  24.  
  25. [cmdletbinding()] 
  26. Param( 
  27. [string] $User     = $(Throw 'No user id specified'), 
  28. [string] $Password = $(Throw 'No password specified') 
  29. ) 
  30.  
  31. # Define registry path for autologon 
  32. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  33.  
  34. # Set autologon 
  35. New-ItemProperty -Path $RegPath -Name AutoAdminLogon -Value 1 -EA 0 
  36.  
  37. # Set userid and password 
  38. New-ItemProperty -Path $RegPath -Name DefaultUserName -Value $User -EA 0 
  39. New-ItemProperty -Path $regPath -Name DefaultPassword -Value $Password -EA 0 
  40.  
  41. # Say nice things and exit! 
  42. Write-Host ("Auto logon set for [{0}] with password: [{1}]" -f $user, $password) 
  43. } 
  44.  
  45. Set-AdminLogon -User 'Cookham\tfl' -Password 'JerryGarciaR0cks' 
Technorati Tags: ,

Friday, 23 November 2012

Convert-PptxToPDF.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This function converts a PPTx file into a PDF file 
  4. .DESCRIPTION 
  5.     The Convert-PptxToPDF function first creates an  
  6.     instance of PowerPoint, opens the $ifile and saves 
  7.     this to $ofile as a PDF file. 
  8. .NOTES 
  9.     File Name  : Convert-PptxToPDF 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0, Office 2010 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.      
  16. .EXAMPLE 
  17.     There is nothing to see, except a set of new PDF Files in the output folder         
  18.  
  19. #> 
  20.  
  21. Function Convert-PptxToPDF { 
  22.  
  23. [CmdletBinding()] 
  24. Param( 
  25. $IFile, 
  26. $OFile 
  27. ) 
  28.  
  29. # add key assemblies 
  30. Add-type -AssemblyName office -ErrorAction SilentlyContinue 
  31. Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 
  32.  
  33. # Open PowerPoint 
  34. $ppt = new-object -com powerpoint.application 
  35. $ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 
  36.  
  37.  
  38. # Open the $Ifile presentation 
  39. $pres = $ppt.Presentations.Open($ifile) 
  40.  
  41. # Now save it away as PDF 
  42. $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
  43. $pres.SaveAs($ofile,$opt) 
  44.  
  45. # and Tidy-up 
  46. $pres.Close() 
  47. $ppt.Quit() 
  48. $ppt=$null 
  49.  
  50. } 
  51.  
  52.  
  53. # Test it 
  54.  
  55. $ipath = "E:\SkyDrive\PowerShell V3 Geek Week\" 
  56.  
  57. Foreach ($ifile in $(ls $ipath -Filter "*.pptx")) { 
  58.   # Build name of output file 
  59.   $pathname = split-path $ifile 
  60.   $filename = split-path $ifile -leaf  
  61.   $file     = $filename.split(".")[0] 
  62.   $ofile    = $pathname + $file + ".pdf" 
  63.  
  64.   # Convert _this_ file to PDF 
  65.    Convert-PptxToPDF -ifile $ifile -OFile $ofile 
  66. } 
Technorati Tags: ,,,

Wednesday, 3 October 2012

Get-GuiMode.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the current GUI mode of a Windows 
  4.     Server 2012 system, including current display resolution. 
  5. .DESCRIPTION 
  6.     This script creates a custom object, then populates it 
  7.     with the current mode (Server Core, MinShell, Full Shell) 
  8.     and with the current resolution (resolution plus h/w). The 
  9.     display information object is then returned. 
  10. .NOTES 
  11.     File Name  : Get-GuiMode.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 3.0  
  14.                  Windows Server 2012 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     Psh>  ./Get-GuiMode 
  20.     ComputerName : S1 
  21.     ServerCore   : True 
  22.     FullServer   : False 
  23.     MinShell     : False 
  24.     Resolution   : 1024x768 
  25.     Height       : 768 
  26.     Width        : 1024 
  27.  
  28. #> 
  29.  
  30.  
  31. Function Get-GUIMode { 
  32.  
  33. # Create a new object to return 
  34. $guimode = new-object psobject 
  35.  
  36. # Add ComputerName to the object 
  37. $guimode |Add-Member -MemberType NoteProperty -Name ComputerName -Value $(hostname) 
  38.  
  39.  
  40. # now determine what's installed 
  41. $sgs  = (Get-WindowsFeature Server-Gui-Shell).Installed 
  42. $mif  = (Get-WindowsFeature Server-Gui-Mgmt-Infra).Installed 
  43.  
  44. If (!$sgs -and !$mif)  # True Server Core  
  45.   { 
  46.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $True 
  47.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False 
  48.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $False 
  49.   } 
  50. Elseif ($sgs -and !$mif) # MinShell 
  51.   { 
  52.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False 
  53.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False 
  54.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $True 
  55.   } 
  56. Elseif ($sgs -and $mif) 
  57.   { 
  58.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False 
  59.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $True 
  60.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $False 
  61.   } 
  62.  
  63. # now resolution 
  64. If ($rx=get-command Get-DisplayResolution) { 
  65.   $res  = (Get-DisplayResolution)[0] 
  66.   $reslen = $res.length 
  67.   $r = [string]"" 
  68.   for ($i = 0; $i -lt $reslen; $i++) 
  69.    {  
  70.      If ($res.substring($i,1) -ne "") { $r += $res.substring($i,1) } 
  71.    } 
  72.  
  73.     
  74.   $guimode |Add-Member -MemberType NoteProperty -Name Resolution -Value $r 
  75.  
  76.   $h = $r.split("x")[1]  # height 
  77.   $w = $r.split("x")[0]  #  
  78.    
  79.   $guimode |Add-Member -MemberType NoteProperty -Name Height -Value $h 
  80.   $guimode |Add-Member -MemberType NoteProperty -Name Width  -Value $w 
  81. } 
  82.  
  83. # Ok - squirt out what we have! 
  84. $guimode 
  85. } 
  86.  
  87. # Here Test it out 
  88. Get-GUiMode 

Set-PowerShellAsShell

  1. <# 
  2. .Synopsis 
  3.    Creates a function to set PowerShell as GUI in Server 2012 
  4. .DESCRIPTION 
  5.    The function in this script sets PowerShell as the  
  6.    default shell in Server 2012. When the server is rebooted, 
  7.    it runs PowerShell.exe by default. When PowerShell starts, it 
  8.    displays the $PSVersionTable variable. 
  9. .NOTES 
  10.     File Name   : Set-PowerShellAsGui.ps1 
  11.     Author      : Thomas Lee - tfl@psp.co.uk 
  12.     Requires    : Server 2012 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com   
  16. .EXAMPLE 
  17.     Left as an exercise to the reader 
  18. #> 
  19.  
  20.  
  21. Function Set-PowerShellAsShell { 
  22.  
  23. [CmdletBinding()] 
  24. Param ( 
  25. [switch] $Reboot = $false 
  26. ) 
  27.  
  28. # Create Registry Path variable 
  29. $RegPath =  "Microsoft.PowerShell.Core\Registry::"  
  30. $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" 
  31. $RegPath += "Windows NT\CurrentVersion\winlogon" 
  32.  
  33. # Create splatted parameter hash table 
  34. $parm  =  @{Path    = $regpath}           # key 
  35. $parm +=  @{Name    = 'Shell'}            # value name 
  36. $parm +=  @{Value   = 'PowerShell.exe –NoExit 
               -Command "
    $psversiontable"'}   # value’s value 
  37.  
  38. # Set Registry value entry 
  39. Set-ItemProperty @parm  
  40.  
  41. # And restart to see PowerShell 
  42. if ($Reboot) {Restart-Computer -confirm} 
  43. }