Utils/AdminTools.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
    .NOTES
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.156
     Created on: 1/28/2019 12:23
     Created by: jisodl0
     Organization: J.B. Hunt
     Filename: AdminTools.ps1
    ===========================================================================
    .DESCRIPTION
        Contains various functions for admin use in PowerShell.
#>


<#
.SYNOPSIS
  Launch a PowerShell console as an Administrator.

.DESCRIPTION
  Launches a PowerShell console as an Administrator by using "Start-Process
PowerShell -Verb runAs".

.EXAMPLE
     PS C:\> Start-PowerShellAsAdmin
#>

function Start-PowerShellAsAdmin {
  [CmdletBinding()]
  [Alias('AdminPosh')]
  param ()
  
  Start-Process PowerShell -Verb runAs
}

<#
.SYNOPSIS
  Determines if the current process has administrator rights.
.DESCRIPTION
  Determines if the current process has administrator rights and returns true or
false.
.EXAMPLE
  PS C:\> Get-IsUserAdmin
  False
.EXAMPLE
  PS C:\> AmIAdmin
  True
.NOTES
  Whether or not the process is an admin is done by checking if the current
  identity is part of the built in Windows Administrator role.
#>

function Get-IsUserAdmin {
  [CmdletBinding()]
  [Alias('AmIAdmin', 'IsAdmin')]
  param ()

  return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator")
}