Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. function Restart-Host
  2. {
  3. [CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
  4.  
  5. Param(
  6. [switch]$AsAdministrator,
  7. [switch]$Force
  8. )
  9.  
  10. $proc = Get-Process -Id $PID
  11. $cmdArgs = [Environment]::GetCommandLineArgs() | Select-Object -Skip 1
  12.  
  13. $params = @{ FilePath = $proc.Path }
  14. if ($AsAdministrator) { $params.Verb = 'runas' }
  15. if ($cmdArgs) { $params.ArgumentList = $cmdArgs }
  16.  
  17. if ($Force -or $PSCmdlet.ShouldProcess($proc.Name,"Restart the console"))
  18. {
  19. if ($host.Name -eq 'Windows PowerShell ISE Host' -and $psISE.PowerShellTabs.Files.IsSaved -contains $false)
  20. {
  21. if ($Force -or $PSCmdlet.ShouldProcess('Unsaved work detected?','Unsaved work detected. Save changes?','Confirm'))
  22. {
  23. foreach ($IseTab in $psISE.PowerShellTabs)
  24. {
  25. $IseTab.Files | ForEach-Object {
  26.  
  27. if ($_.IsUntitled -and !$_.IsSaved)
  28. {
  29. $_.SaveAs($_.FullPath,[System.Text.Encoding]::UTF8)
  30. }
  31. elseif(!$_.IsSaved)
  32. {
  33. $_.Save()
  34. }
  35. }
  36. }
  37. }
  38. else
  39. {
  40. foreach ($IseTab in $psISE.PowerShellTabs)
  41. {
  42. $unsavedFiles = $IseTab.Files | Where-Object IsSaved -eq $false
  43. $unsavedFiles | ForEach-Object {$IseTab.Files.Remove($_,$true)}
  44. }
  45. }
  46. }
  47.  
  48. Start-Process @params
  49. $proc.CloseMainWindow()
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement