├── PSSystemAdministrator.psm1 └── README.md /PSSystemAdministrator.psm1: -------------------------------------------------------------------------------- 1 | <# 2 | This module is meant to be used in a Windows Domain by a domain administrator with PowerShell 7. 3 | 4 | By: 5 | Ben Peterson 6 | linkedin.com/in/benponline 7 | github.com/benponline 8 | twitter.com/benponline 9 | paypal.me/teknically 10 | #> 11 | 12 | function Add-DHCPReservation{ 13 | <# 14 | .SYNOPSIS 15 | Adds a reservation in DHCP for a computer. 16 | 17 | .DESCRIPTION 18 | This function adds an IPv4 reservation for a computer in all DHCP servers in a domain. 19 | 20 | .PARAMETER ComputerName 21 | Name of the computer that will get a reservation in DHCP. 22 | 23 | .PARAMETER IPAddress 24 | IP address of the reservation made in DHCP. 25 | 26 | .INPUTS 27 | None. 28 | 29 | .OUTPUTS 30 | None. 31 | 32 | .NOTES 33 | This function is meant to be used in a domain with one DHCP scope. 34 | 35 | This function will attempt to add the reservation to each DHCP server in the domain. 36 | 37 | The target computer will not get the new address immediately if it does not already use that IP. 38 | 39 | You can create a reservation with an IP that is currently leased to another computer. You will need to manually trigger an IP change in the computer currently holding the IP, then in the computer getting the new reservation. The computer in the reservation will then be using the assigned IP. 40 | 41 | You can allow the DHCP servers to naturally renew their leases over time. Eventually the computer will get the IP assigned to it in the reservation. 42 | 43 | If a reservation with the computer or IP address passed to the function already exists, then the function will stop and notify you which DHCP server it is located on. 44 | 45 | If this function fails because it is unable to run the Get-DhcpServerInDC command, then run 'Import-Module DhcpServer -SkipEditionCheck' in Powershell to install the needed module. 46 | 47 | .EXAMPLE 48 | Add-DHCPReservation -ComputerName "Computer1" -IPAddress 10.10.10.123 49 | 50 | This will create a reservation in all available DHCP servers in a domain for the computer name passed to it for the IP address passed to it. 51 | 52 | .LINK 53 | By Ben Peterson 54 | linkedin.com/in/benponline 55 | github.com/benponline 56 | twitter.com/benponline 57 | paypal.me/teknically 58 | #> 59 | 60 | [cmdletbinding(SupportsShouldProcess)] 61 | param( 62 | [Parameter(Mandatory=$true)] 63 | [string]$ComputerName, 64 | 65 | [Parameter(Mandatory=$true)] 66 | [string]$IPAddress, 67 | 68 | [string]$Description = "" 69 | ) 70 | 71 | Import-Module DhcpServer -SkipEditionCheck 72 | $dhcpServers = (Get-DhcpServerInDC).DnsName 73 | $hostName = (Get-ADComputer -Identity $ComputerName).DNSHostName 74 | 75 | $reservationExists = $false 76 | foreach($server in $dhcpServers){ 77 | $dhcpServer = $server.split(".")[0] 78 | $scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId 79 | $reservations = Get-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer 80 | 81 | # Check for reservations that already contain the host name or ip address passed to the funciton. 82 | foreach($r in $reservations){ 83 | if($r.Name -EQ $hostName){ 84 | Write-Host "The computer $hostName already has a reservation on $dhcpServer" 85 | $reservationExists = $true 86 | } 87 | 88 | if($r.IPAddress -EQ $IPAddress){ 89 | Write-Host "The IP address $IPAddress already has a reservation on $dhcpServer" 90 | $reservationExists = $true 91 | } 92 | } 93 | } 94 | 95 | if($reservationExists -eq $true){ 96 | return 97 | } 98 | 99 | foreach($server in $dhcpServers){ 100 | $dhcpServer = $server.split(".")[0] 101 | $scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId 102 | $dhcpLeases = Get-DhcpServerv4Lease -ComputerName $dhcpServer -ScopeId $scopeId -AllLeases 103 | 104 | $clientId = ($dhcpLeases | Where-Object -Property HostName -EQ $hostName | Select-Object -First 1).ClientId 105 | 106 | if ($PSCmdlet.ShouldProcess($server, "Adding dhcp reservation for $ComputerName for $IPAddress")) { 107 | Add-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer -IPAddress $IPAddress -ClientId $clientId -Description $Description -Name $hostName 108 | } 109 | } 110 | } 111 | 112 | function Disable-Computer{ 113 | <# 114 | .SYNOPSIS 115 | Disables a computer. 116 | 117 | .DESCRIPTION 118 | Disables a computer or group of computers by passing host names or computer AD objects to this function. 119 | 120 | .PARAMETER Name 121 | This is the host name of the computer that will be disable. 122 | 123 | .INPUTS 124 | Computer AD objects can be passed to this function from the pipeline. 125 | 126 | .OUTPUTS 127 | None. 128 | 129 | .NOTES 130 | 131 | .EXAMPLE 132 | Disable-Computer -Name "Computer1" 133 | 134 | Disables the computer named "Computer1" in Active Directory. 135 | 136 | .EXAMPLE 137 | "Computer1","Computer2" | Disable-Computer 138 | 139 | Disables computers Computer1 and Computer2 in Active Directory. 140 | 141 | .EXAMPLE 142 | Get-ADComputer Computer1 | Disable-Computer 143 | 144 | Disables Computer1 in Active Directory. 145 | 146 | .LINK 147 | By Ben Peterson 148 | linkedin.com/in/benponline 149 | github.com/benponline 150 | twitter.com/benponline 151 | paypal.me/teknically 152 | #> 153 | 154 | [cmdletbinding(SupportsShouldProcess)] 155 | param( 156 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 157 | [Alias('ComputerName')] 158 | [string]$Name 159 | ) 160 | 161 | begin{ 162 | $computers = @() 163 | } 164 | 165 | process{ 166 | $computers += $Name 167 | } 168 | 169 | end{ 170 | foreach ($computer in $computers){ 171 | 172 | if($PSCmdlet.ShouldProcess($computer)){ 173 | $computer | Get-ADComputer | Disable-ADAccount 174 | } 175 | 176 | } 177 | } 178 | } 179 | 180 | function Disable-User{ 181 | <# 182 | .SYNOPSIS 183 | Disables a user. 184 | 185 | .DESCRIPTION 186 | Disables a user or group of users by passing SamAccountName or user AD objects to this funtion. 187 | 188 | .PARAMETER SamAccountName 189 | This is the user name of the user that will be disabled. 190 | 191 | .INPUTS 192 | User AD objects can be passed to this function. 193 | 194 | .OUTPUTS 195 | None. 196 | 197 | .NOTES 198 | 199 | .EXAMPLE 200 | Disable-User -Name "User1" 201 | 202 | Disables the user named User1 in Active Directory. 203 | 204 | .EXAMPLE 205 | "User1","User2" | Disable-User 206 | 207 | Disables users User1 and User2 in Active Directory. 208 | 209 | .EXAMPLE 210 | Get-ADUser "User1" | Disable-User 211 | 212 | Disables User1 in Active Directory. 213 | 214 | .LINK 215 | By Ben Peterson 216 | linkedin.com/in/benponline 217 | github.com/benponline 218 | twitter.com/benponline 219 | paypal.me/teknically 220 | #> 221 | 222 | [cmdletbinding(SupportsShouldProcess)] 223 | param( 224 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 225 | [string]$SamAccountName 226 | ) 227 | 228 | begin{ 229 | $users = @() 230 | } 231 | 232 | process{ 233 | $users += $SamAccountName 234 | } 235 | 236 | end{ 237 | foreach ($user in $users){ 238 | if($PSCmdlet.ShouldProcess($user)){ 239 | $user | Get-ADUser | Disable-ADAccount 240 | } 241 | } 242 | } 243 | } 244 | 245 | function Enable-Computer{ 246 | <# 247 | .SYNOPSIS 248 | Enables a computer. 249 | 250 | .DESCRIPTION 251 | Enables a computer or group of computers by passing host names or computer AD objects to this function. 252 | 253 | .PARAMETER Name 254 | This is the host name of the computer that will be enable. 255 | 256 | .INPUTS 257 | Computer AD objects can be passed to this function from the pipeline. 258 | 259 | .OUTPUTS 260 | None. 261 | 262 | .NOTES 263 | 264 | .EXAMPLE 265 | Enable-Computer -Name "Computer1" 266 | 267 | Enables the computer named "Computer1" in Active Directory. 268 | 269 | .EXAMPLE 270 | "Computer1","Computer2" | Enable-Computer 271 | 272 | Enables computers Computer1 and Computer2 in Active Directory. 273 | 274 | .EXAMPLE 275 | Get-ADComputer Computer1 | Enable-Computer 276 | 277 | Enables Computer1 in Active Directory. 278 | 279 | .LINK 280 | By Ben Peterson 281 | linkedin.com/in/benponline 282 | github.com/benponline 283 | twitter.com/benponline 284 | paypal.me/teknically 285 | #> 286 | 287 | [cmdletbinding(SupportsShouldProcess)] 288 | param( 289 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 290 | [Alias('ComputerName')] 291 | [string]$Name 292 | ) 293 | 294 | begin{ 295 | $computers = @() 296 | } 297 | 298 | process{ 299 | $computers += $Name 300 | } 301 | 302 | end{ 303 | foreach ($computer in $computers){ 304 | 305 | if($PSCmdlet.ShouldProcess($computer)){ 306 | $computer | Get-ADComputer | Enable-ADAccount 307 | } 308 | } 309 | } 310 | } 311 | 312 | function Enable-User { 313 | <# 314 | .SYNOPSIS 315 | Enables a user. 316 | 317 | .DESCRIPTION 318 | Enables a user or group of users by passing SamAccountNames or user AD objects to this funtion. 319 | 320 | .PARAMETER SamAccountName 321 | This is the user name of the user that will be enabled. 322 | 323 | .INPUTS 324 | User AD objects can be passed to this function. 325 | 326 | .OUTPUTS 327 | None. 328 | 329 | .NOTES 330 | 331 | .EXAMPLE 332 | Enable-User -SamAccountName "User1" 333 | 334 | Enables the user named User1 in Active Directory. 335 | 336 | .EXAMPLE 337 | "User1","User2" | Enable-User 338 | 339 | Enables users User1 and User2 in Active Directory. 340 | 341 | .EXAMPLE 342 | Get-ADUser "User1" | Enable-User 343 | 344 | Enables User1 in Active Directory. 345 | 346 | .LINK 347 | By Ben Peterson 348 | linkedin.com/in/benponline 349 | github.com/benponline 350 | twitter.com/benponline 351 | paypal.me/teknically 352 | #> 353 | 354 | [cmdletbinding(SupportsShouldProcess)] 355 | param( 356 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 357 | [string]$SamAccountName 358 | ) 359 | 360 | begin{ 361 | $users = @() 362 | } 363 | 364 | process{ 365 | $users += $SamAccountName 366 | } 367 | 368 | end{ 369 | foreach ($user in $users){ 370 | 371 | if($PSCmdlet.ShouldProcess($user)){ 372 | $user | Get-ADUser | Enable-ADAccount 373 | } 374 | } 375 | } 376 | } 377 | 378 | function Enable-WakeOnLan{ 379 | <# 380 | .SYNOPSIS 381 | Configures a computer to allow wake on lan. 382 | 383 | .DESCRIPTION 384 | Configures a computer's ethernet network adapter to respond to wake on lan commands. This allow the computer to be turned on while it is shut down. 385 | 386 | .PARAMETER Name 387 | Target computer's host name. 388 | 389 | .INPUTS 390 | Computer AD objects 391 | 392 | .OUTPUTS 393 | None. 394 | 395 | .NOTES 396 | This function needs to be run against a computer before you can be sure that the Start-Computer function in the PSSystemAdministrator modile will work. 397 | 398 | .EXAMPLE 399 | Enable-WakeOnLan -Name 'Computer1' 400 | 401 | Sets the network adapter on 'Computer1' to respond to WOL commands and boot from a shutdown state. 402 | 403 | .EXAMPLE 404 | 'Computer1','Computer2' | Enable-WakeOnLan 405 | 406 | Sets the network adapters on 'Computer1' and 'Computer2' to respond to WOL commands and boot from a shutdown state. 407 | 408 | .EXAMPLE 409 | Get-OUComputer -OrganizationalUnit 'Department X' | Enable-WakeOnLan 410 | 411 | Sets the network adapters on all computers in the 'Department X' OU to respond to WOL commands and boot from a shutdown state. 412 | 413 | .LINK 414 | By Ben Peterson 415 | linkedin.com/in/benponline 416 | github.com/benponline 417 | twitter.com/benponline 418 | paypal.me/teknically 419 | Based on: https://docs.microsoft.com/en-us/powershell/module/netadapter/enable-netadapterpowermanagement 420 | #> 421 | 422 | [CmdletBinding(SupportsShouldProcess)] 423 | param( 424 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 425 | [Alias("ComputerName")] 426 | [string]$Name 427 | ) 428 | 429 | begin{ 430 | $domainController = (Get-ADDomainController).Name 431 | $scopeID = (Get-DhcpServerv4Scope -ComputerName $domainController).ScopeID 432 | } 433 | 434 | process{ 435 | $cimSession = New-CimSession -ComputerName $Name 436 | $computerMAC = (Get-DhcpServerv4Lease -ComputerName $domainController -ScopeId $scopeID | Where-Object -Property hostname -match $Name).clientid 437 | $adapterName = (Get-NetAdapter -CimSession $cimSession | Where-Object -Property MacAddress -match $computerMAC).Name 438 | 439 | if($PSCmdlet.ShouldProcess($Name)){ 440 | Enable-NetAdapterPowerManagement -CimSession $cimSession -Name $adapterName -WakeOnMagicPacket 441 | } 442 | } 443 | 444 | end{} 445 | } 446 | 447 | function Get-AccessedFile{ 448 | <# 449 | .SYNOPSIS 450 | Gets all files in a directory that have been accessed in the last 24 hours. 451 | 452 | .DESCRIPTION 453 | Gets all files in a directory recursively that have been accessed less than a day ago. Directory and days in the past can be adjusted. 454 | 455 | .PARAMETER Path 456 | Function will gather all files recursively from the directory at the end of the path. 457 | 458 | .PARAMETER Days 459 | Function will return only files that have been accessed this number of days into the past. 460 | 461 | .INPUTS 462 | You can pipe multiple paths to this function. 463 | 464 | .OUTPUTS 465 | Array of PS objects that includes file Name, LastAccessTme, SizeMB, and FullName. 466 | 467 | .NOTES 468 | 469 | .EXAMPLE 470 | Get-AccessedFile -Path "C:\Directory1" -Days 471 | 472 | Gets all files recursively in the "Directory1" folder that have been accessed within 5 days. 473 | 474 | .EXAMPLE 475 | "C:\Directory1","C:\Directory2" | Get-AccessedFile 476 | 477 | Gets all files recursively in the "Directory1" and "Directory2" folders that have been accessed in the last day. 478 | 479 | .LINK 480 | By Ben Peterson 481 | linkedin.com/in/benponline 482 | github.com/benponline 483 | twitter.com/benponline 484 | paypal.me/teknically 485 | #> 486 | 487 | [cmdletbinding()] 488 | param( 489 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 490 | [Alias('FullName')] 491 | [string]$Path, 492 | [int]$Days = 1 493 | ) 494 | 495 | begin{ 496 | $files = @() 497 | $fileAge = (Get-Date).AddDays(-1 * $Days) 498 | } 499 | 500 | process{ 501 | $files += Get-ChildItemLastAccessTime -Path $Path | 502 | Where-Object -Property LastAccessTime -GT $fileAge 503 | } 504 | 505 | end{ 506 | return $files 507 | } 508 | } 509 | 510 | function Get-ActiveComputer{ 511 | <# 512 | .SYNOPSIS 513 | Gets a list of computers that have logged onto the domain in the last 30 days. 514 | 515 | .DESCRIPTION 516 | Gets a list of computers from Active Directory that have logged onto the domain in the last 30 days. By default all computers are checked, but can be limited to a specific organizational unit. Days inactive can be adjusted. 517 | 518 | .PARAMETER Days 519 | Sets how recently in days the computer account has to be active for it to be returned. 520 | 521 | .PARAMETER OrganizationalUnit 522 | Limits the function to a specific organizational unit. 523 | 524 | .INPUTS 525 | None. 526 | 527 | .OUTPUTS 528 | Computer AD object for each computer returned. 529 | 530 | .NOTES 531 | 532 | .EXAMPLE 533 | Get-ActiveComputer 534 | 535 | Lists all computers in the domain that have been online in the last 30 days. 536 | 537 | .EXAMPLE 538 | Get-ActiveComputer -Days 100 539 | 540 | Lists all computers in the domain that have been online in the last 100 days. 541 | 542 | .LINK 543 | By Ben Peterson 544 | linkedin.com/in/benponline 545 | github.com/benponline 546 | twitter.com/benponline 547 | paypal.me/teknically 548 | #> 549 | 550 | [CmdletBinding()] 551 | Param( 552 | [int]$Days = 30, 553 | [string]$OrganizationalUnit = "" 554 | ) 555 | 556 | if($OrganizationalUnit -eq ""){ 557 | $computers = Get-ADComputer -Filter * | 558 | Get-ComputerLastLogonTime | 559 | Where-Object -Property LastLogon -GT ((Get-Date).AddDays(($Days * -1))) 560 | }else{ 561 | $computers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit | 562 | Get-ComputerLastLogonTime | 563 | Where-Object -Property LastLogon -GT ((Get-Date).AddDays(($Days * -1))) 564 | } 565 | 566 | return $computers 567 | } 568 | 569 | function Get-ActiveFile{ 570 | <# 571 | .SYNOPSIS 572 | Gets all files in a directory that have been written in the last 24 hours. 573 | 574 | .DESCRIPTION 575 | Gets all files in a directory recursively that have been written to going back one day. Path and days back can be adjusted. 576 | 577 | .PARAMETER Path 578 | Sets directory the function returns files from. 579 | 580 | .PARAMETER Days 581 | Sets how recently, in days, the file has been written to. 582 | 583 | .INPUTS 584 | You can pipe multiple paths to this function. 585 | 586 | .OUTPUTS 587 | Array of PS objects that includes file Name, LastWriteTime, SizeMB, and FullName. 588 | 589 | .NOTES 590 | 591 | .EXAMPLE 592 | Get-ActiveFile -Path "C:\Directory1" -Days 5 593 | 594 | Gathers all files recursively in the "Directory1" folder that have been written to within 5 days. 595 | 596 | .EXAMPLE 597 | "C:\Directory1","C:\Directory2" | Get-ActiveFile 598 | 599 | Gathers all files recursively in the "Directory1" and "Directory2" folders that have been written to in the last day. 600 | 601 | .LINK 602 | By Ben Peterson 603 | linkedin.com/in/benponline 604 | github.com/benponline 605 | twitter.com/benponline 606 | paypal.me/teknically 607 | #> 608 | 609 | [cmdletbinding()] 610 | param( 611 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 612 | [string]$Path, 613 | [int]$Days = 1 614 | ) 615 | 616 | begin{ 617 | $files = @() 618 | $fileAge = (Get-Date).AddDays(-1*$Days) 619 | } 620 | 621 | process{ 622 | $files += Get-ChildItemLastWriteTime -Path $Path | 623 | Where-Object -Property LastWriteTime -GT $fileAge 624 | } 625 | 626 | end{ 627 | return $files 628 | } 629 | } 630 | 631 | function Get-ActiveUser{ 632 | <# 633 | .SYNOPSIS 634 | Gets a list of all users that have logged on in the last 30 days. 635 | 636 | .DESCRIPTION 637 | Gets a list of all users in active directory that have logged on in the last 30 days. Days and Organizational Unit can be adjusted. 638 | 639 | .PARAMETER Days 640 | Sets how long the user account has to be inactive for it to be returned. 641 | 642 | .PARAMETER OrganizationalUnit 643 | Focuses the function on a specific AD organizational unit. 644 | 645 | .INPUTS 646 | None. 647 | 648 | .OUTPUTS 649 | PS objects with user names and last logon date. 650 | 651 | .NOTES 652 | Function is intended to help find inactive user accounts. 653 | 654 | .EXAMPLE 655 | Get-ActiveUser 656 | 657 | Lists all users in the domain that have not checked in for more than 3 months. 658 | 659 | .EXAMPLE 660 | Get-ActiveUser -Days 2 661 | 662 | Lists all users in the domain that have not checked in for more than 2 days. 663 | 664 | .EXAMPLE 665 | Get-ActiveUser -Days 45 -OrganizationalUnit "Company Servers" 666 | 667 | Lists all users in the domain that have not checked in for more than 45 days in the "Company Servers" organizational unit. 668 | 669 | .LINK 670 | By Ben Peterson 671 | linkedin.com/in/benponline 672 | github.com/benponline 673 | twitter.com/benponline 674 | paypal.me/teknically 675 | #> 676 | 677 | [CmdletBinding()] 678 | Param( 679 | [int]$Days = 30, 680 | [string]$OrganizationalUnit = "" 681 | ) 682 | 683 | if($OrganizationalUnit -eq ""){ 684 | $users = Get-ADUser -Filter * | 685 | Get-UserLastLogonTime | 686 | Where-Object -Property LastLogon -GT ((Get-Date).AddDays($Days * -1)) 687 | }else{ 688 | $users = Get-OUUser -OrganizationalUnit $OrganizationalUnit | 689 | Get-UserLastLogonTime | 690 | Where-Object -Property LastLogon -GT ((Get-Date).AddDays($Days * -1)) 691 | } 692 | 693 | return $users 694 | } 695 | 696 | function Get-ChildItemLastAccessTime{ 697 | <# 698 | .SYNOPSIS 699 | Gets all files in a directory and returns information including file name and last access time. 700 | 701 | .DESCRIPTION 702 | Gets all files in a directory recursively and returns file name, last access time, size in MB, and full name. 703 | 704 | .PARAMETER Path 705 | Function will gather all files recursively from this directory. 706 | 707 | .INPUTS 708 | You can pipe multiple paths to this function. 709 | 710 | .OUTPUTS 711 | Array of PS objects that includes FileNames, LastAccessTime, SizeMB, and FullName. 712 | 713 | .NOTES 714 | 715 | .EXAMPLE 716 | Get-ChildItemLastAccessTime -Path "C:\Directory1" 717 | 718 | Gathers information on all files recursively in the "Directory1" directory. 719 | 720 | .EXAMPLE 721 | "C:\Directory1","C:\Directory2" | Get-ChildItemLastAccessTime 722 | 723 | Gathers all files recursively in the "Directory1" and "Directory2" folders. 724 | 725 | .LINK 726 | By Ben Peterson 727 | linkedin.com/in/benponline 728 | github.com/benponline 729 | twitter.com/benponline 730 | paypal.me/teknically 731 | #> 732 | 733 | [cmdletbinding()] 734 | param( 735 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 736 | [string]$Path 737 | ) 738 | 739 | begin{ 740 | $files = @() 741 | } 742 | 743 | process{ 744 | $files += Get-ChildItem -Path $Path -File -Recurse | Get-ItemLastAccessTime 745 | } 746 | 747 | end{ 748 | return $files 749 | } 750 | } 751 | 752 | function Get-ChildItemLastWriteTime{ 753 | <# 754 | .SYNOPSIS 755 | Gets all files in a directory and returns information including last write time. 756 | 757 | .DESCRIPTION 758 | This function gathers all files in a directory recursively. Returns file name, last write time, size in MB, and full name. 759 | 760 | .PARAMETER Path 761 | Function will gather all files recursively from this directory. 762 | 763 | .INPUTS 764 | None. 765 | 766 | .OUTPUTS 767 | Array of PS objects that includes file Name, LastWriteTime, SizeMB, and FullName. 768 | 769 | .NOTES 770 | 771 | .EXAMPLE 772 | Get-ChildItemLastWriteTime -Path "C:\Directory1" 773 | 774 | Gathers all files recursively in the "Directory1" folder and returns file names, last write time, size in MB, and full name. 775 | 776 | .EXAMPLE 777 | "C:\Directory1","C:\Directory2" | Get-ChildItemLastWriteTime 778 | 779 | Gathers all files recursively in the "Directory1" and "Directory2" folders. 780 | 781 | .LINK 782 | By Ben Peterson 783 | linkedin.com/in/benponline 784 | github.com/benponline 785 | twitter.com/benponline 786 | paypal.me/teknically 787 | #> 788 | 789 | [cmdletbinding()] 790 | param( 791 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 792 | [string]$Path 793 | ) 794 | 795 | begin{ 796 | $files = @() 797 | } 798 | 799 | process{ 800 | $files += Get-ChildItem -Path $Path -File -Recurse | Get-ItemLastWriteTime 801 | } 802 | 803 | end{ 804 | return $files 805 | } 806 | } 807 | 808 | function Get-ComputerCurrentUser{ 809 | <# 810 | .SYNOPSIS 811 | Gets the current user logged onto a computer. 812 | 813 | .DESCRIPTION 814 | Gets the currently logged in user on a computer or computers. 815 | 816 | .PARAMETER Name 817 | The host name of the computer that the current user will be returned from. 818 | 819 | .INPUTS 820 | An array of host names or AD Computer objects. 821 | 822 | .OUTPUTS 823 | Returns a PS Object with the computer Name and CurrentUser. 824 | 825 | .NOTES 826 | 827 | .EXAMPLE 828 | Get-ComputerCurrentUser 829 | 830 | Gets the current user on the local computer. 831 | 832 | .EXAMPLE 833 | Get-ComputerCurrentUser -Name 'Computer1' 834 | 835 | Gets the current logged on user from 'Computer1'. 836 | 837 | .EXAMPLE 838 | 'Computer1','Computer2' | Get-ComputerCurrentUser 839 | 840 | Returns the current logged on user from 'Computer1' and 'Computer2'. 841 | 842 | .EXAMPLE 843 | Get-OUComputer -OrganizationalUnit 'Department X' | Get-ComputerCurrentUser 844 | 845 | Returns the current logged on users for all computer in the 'Deparment X' Active Directory organizational unit. Get-OUComputer is a function from the PSSystemAdministrator module. 846 | 847 | .LINK 848 | By Ben Peterson 849 | linkedin.com/in/benponline 850 | github.com/benponline 851 | twitter.com/benponline 852 | paypal.me/teknically 853 | #> 854 | 855 | [CmdletBinding()] 856 | Param( 857 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 858 | [Alias('ComputerName')] 859 | [string]$Name = $env:COMPUTERNAME 860 | ) 861 | 862 | begin{ 863 | $computerUserList = @() 864 | } 865 | 866 | process{ 867 | $currentUser = (Get-CimInstance -ComputerName $Name -ClassName Win32_ComputerSystem).UserName 868 | 869 | if(!$null -eq $currentUser){ 870 | $currentUser = $currentUser.split('\')[-1] 871 | }else{ 872 | $currentUser = "" 873 | } 874 | 875 | $computerUserList += [PSCustomObject]@{ 876 | ComputerName = $Name; 877 | UserName = $currentUser 878 | } 879 | } 880 | 881 | end{ 882 | return $computerUserList 883 | } 884 | } 885 | 886 | function Get-ComputerDriveInformation{ 887 | <# 888 | .SYNOPSIS 889 | Gets information about the drives on a computer. 890 | 891 | .DESCRIPTION 892 | Returns information about the drives on a computer or group of computers. The information includes computer name, drive, volume name, size, free space, and if the drive has less than 20% space left. 893 | 894 | .PARAMETER Name 895 | Specifies the computer the function will gather information from. 896 | 897 | .INPUTS 898 | You can pipe host names or AD computer objects. 899 | 900 | .OUTPUTS 901 | Returns PS objects to the host the following information about the drives on a computer: Name, DeviceID, VolumeName,SizeGB, FreeGB, and indicates those under 20% desc space remaining. 902 | 903 | .NOTES 904 | Compatible with Window 7 and newer. 905 | 906 | Results include mapped drives. 907 | 908 | .EXAMPLE 909 | Get-ComputerDriveInformation 910 | 911 | Gets drive information for the local host. 912 | 913 | .EXAMPLE 914 | Get-ComputerDriveInformation -Name computer 915 | 916 | Gets ddrive information for "computer". 917 | 918 | .EXAMPLE 919 | Get-ADComputer -Filter * | Get-ComputerDriveInformation 920 | 921 | Gets drive information for all computers in AD. 922 | 923 | .LINK 924 | By Ben Peterson 925 | linkedin.com/in/benponline 926 | github.com/benponline 927 | twitter.com/benponline 928 | paypal.me/teknically 929 | #> 930 | 931 | [CmdletBinding()] 932 | Param( 933 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 934 | [Alias('ComputerName')] 935 | [string]$Name = $env:COMPUTERNAME 936 | ) 937 | 938 | begin{ 939 | $driveInformationList = @() 940 | } 941 | 942 | process{ 943 | $driveInformationList += Get-CimInstance -ComputerName $Name -ClassName win32_logicaldisk -Property DeviceID,VolumeName,Size,FreeSpace,DriveType | 944 | Where-Object -Property DriveType -EQ 3 | 945 | Select-Object -Property @{n="Computer";e={$Name}},` 946 | @{n="DeviceID";e={$_.deviceid}},` 947 | @{n="VolumeName";e={$_.volumename}},` 948 | @{n="SizeGB";e={$_.size / 1GB -as [int]}},` 949 | @{n="FreeGB";e={$_.freespace / 1GB -as [int]}},` 950 | @{n="Under20Percent";e={if(($_.freespace / $_.size) -le 0.2){"True"}else{"False"}}} 951 | } 952 | 953 | end{ 954 | $driveInformationList = $driveInformationList | Where-Object -Property SizeGB -NE 0 955 | return $driveInformationList 956 | } 957 | } 958 | 959 | function Get-ComputerFailedLogonEvent{ 960 | <# 961 | .SYNOPSIS 962 | Gets failed logon events from a computer in the last day. 963 | 964 | .DESCRIPTION 965 | Gets failed logon events from a computer or computers in the last day. Can adjust how far back in days events are returned. 966 | 967 | .PARAMETER Name 968 | Host name of the computer events will be returned from. 969 | 970 | .PARAMETER Days 971 | Sets how far back in days the function will look for failed logon events. 972 | 973 | .INPUTS 974 | Computer AD objects. 975 | 976 | .OUTPUTS 977 | PS objects for computer failed logon events with Computer, TimeWritten, EventID, InstanceId, and Message. 978 | 979 | .NOTES 980 | Requires administrator privilages. 981 | 982 | Requires "Printer and file sharing", "Network Discovery", and "Remote Registry" to be enabled on computers that are searched. This funtion can take a long time to complete if more than 5 computers are searched. 983 | 984 | .EXAMPLE 985 | Get-ComputerFailedLogonEvent 986 | 987 | This cmdlet returns the last 5 system errors from localhost. 988 | 989 | .EXAMPLE 990 | Get-ComputerFailedLogonEvent -Name "Server" -Days 2 991 | 992 | This function returns the last 2 days of failed logon events from Server. 993 | 994 | .EXAMPLE 995 | "computer1","computer2" | Get-ComputerFailedLogonEvent 996 | 997 | This function returns failed logon events from "computer1" and "computer2" in the last day. 998 | 999 | .EXAMPLE 1000 | Get-ADComputer "Computer1" | Get-ComputerFailedLogonEvent 1001 | 1002 | This cmdlet returns failed logon event from the last day from Computer1. 1003 | 1004 | .LINK 1005 | By Ben Peterson 1006 | linkedin.com/in/benponline 1007 | github.com/benponline 1008 | twitter.com/benponline 1009 | paypal.me/teknically 1010 | Made with help from: https://theposhwolf.com/howtos/Get-ADUserBadPasswords 1011 | #> 1012 | 1013 | [CmdletBinding()] 1014 | Param( 1015 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1016 | [Alias('ComputerName')] 1017 | [string]$Name = "$env:COMPUTERNAME", 1018 | [int]$DaysBack = 1 1019 | ) 1020 | 1021 | begin{ 1022 | $failedLogonList = @() 1023 | $date = (Get-Date).AddDays($DaysBack * -1) 1024 | 1025 | $logonTypeDictionary = @{ 1026 | '2' = 'Interactive' 1027 | '3' = 'Network' 1028 | '4' = 'Batch' 1029 | '5' = 'Service' 1030 | '7' = 'Unlock' 1031 | '8' = 'Networkcleartext' 1032 | '9' = 'NewCredentials' 1033 | '10' = 'RemoteInteractive' 1034 | '11' = 'CachedInteractive' 1035 | } 1036 | } 1037 | 1038 | process{ 1039 | $failedLogonsRaw = Get-WinEvent -ComputerName $Name -FilterHashtable @{LogName='Security';ID=4625; StartTime=$date} 1040 | 1041 | for($i = 0; $i -lt $failedLogonsRaw.Count; $i++){ 1042 | $logonTypeNumber = $failedLogonsRaw[$i].Properties[10].Value 1043 | 1044 | $failedLogonList += [PSCustomObject]@{ 1045 | Computer = $Name; 1046 | Account = $failedLogonsRaw[$i].Properties[5].Value; 1047 | AccountDomain = $failedLogonsRaw[$i].Properties[6].Value; 1048 | LogonType = $logonTypeDictionary["$logonTypeNumber"]; 1049 | TimeCreated = $failedLogonsRaw[$i].TimeCreated 1050 | } 1051 | } 1052 | } 1053 | 1054 | end{ 1055 | return $failedLogonList 1056 | } 1057 | } 1058 | 1059 | function Get-ComputerInformation{ 1060 | <# 1061 | .SYNOPSIS 1062 | Gets general information about a computer. 1063 | 1064 | .DESCRIPTION 1065 | This function gathers information about a computer or computers. By default it gathers info from the local host. 1066 | 1067 | .PARAMETER Name 1068 | Specifies which computer's information is gathered. 1069 | 1070 | .INPUTS 1071 | You can pipe host names or AD computer objects. 1072 | 1073 | .OUTPUTS 1074 | Returns an object with computer Name, Model, Processor, MemoryGB, CDriveGB, CurrentUser, IPAddress, LastBootupTime, and LastLogonTime. 1075 | 1076 | .NOTES 1077 | Compatible for Windows 7 and newer. 1078 | 1079 | Only returns information from computers running Windows 10 or Windows Server 2012 or higher. 1080 | 1081 | Will not return information on computers that are offline. 1082 | 1083 | .EXAMPLE 1084 | Get-ComputerInformation 1085 | 1086 | Returns computer information for the local host. 1087 | 1088 | .EXAMPLE 1089 | Get-ComputerInformation -Name "Server1" 1090 | 1091 | Returns computer information for Server1. 1092 | 1093 | .EXAMPLE 1094 | Get-ADComputer -filter * | Get-ComputerInformation 1095 | 1096 | Returns computer information on all AD computers. 1097 | 1098 | .LINK 1099 | By Ben Peterson 1100 | linkedin.com/in/benponline 1101 | github.com/benponline 1102 | twitter.com/benponline 1103 | paypal.me/teknically 1104 | #> 1105 | 1106 | [CmdletBinding()] 1107 | Param( 1108 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 1109 | [Alias('ComputerName')] 1110 | [string]$Name = $env:COMPUTERNAME 1111 | ) 1112 | 1113 | begin{ 1114 | $computers = [System.Collections.Generic.List[string]]::new() 1115 | $computerInfoList = [System.Collections.Generic.List[psobject]]::new() 1116 | } 1117 | 1118 | process{ 1119 | $computers.Add($Name) 1120 | } 1121 | 1122 | end{ 1123 | $computers | ForEach-Object -Parallel { 1124 | if(Test-Connection -ComputerName $_ -Count 1 -Quiet){ 1125 | New-Object -TypeName PSObject -Property @{ 1126 | Name = $_; 1127 | Model = (Get-ComputerModel -Name $_).Model; 1128 | Processor = (Get-ComputerProcessor -Name $_).Processor; 1129 | MemoryGB = (Get-ComputerMemory -Name $_).MemoryGB; 1130 | CDriveGB = (Get-ComputerDriveInformation -Name $_ | Where-Object -Property DeviceID -Match 'C').SizeGB; 1131 | CurrentUser = (Get-ComputerCurrentUser -Name $_).UserName; 1132 | IPAddress = (Get-ComputerIPAddress -Name $_).IPAddress; 1133 | LastBootupTime = (Get-ComputerLastBootUpTime -Name $_).LastBootupTime; 1134 | LastLogon = "" 1135 | } 1136 | } 1137 | } | ForEach-Object {$computerInfoList.Add($_)} 1138 | 1139 | #Get-ComputerLastLogonTime did not work consistantly inside of Foreach-Object. 1140 | foreach($computer in $computerInfoList){ 1141 | if(Test-Connection -ComputerName $computer.Name -Count 1 -Quiet){ 1142 | $computer.LastLogon = (Get-ComputerLastLogonTime -Name $computer.Name).LastLogon 1143 | } 1144 | } 1145 | 1146 | return $computerInfoList 1147 | } 1148 | } 1149 | 1150 | function Get-ComputerIPAddress{ 1151 | <# 1152 | .SYNOPSIS 1153 | Gets the IPv4 address of a computer. 1154 | 1155 | .DESCRIPTION 1156 | Gets the IPv4 address of a computer or computers. 1157 | 1158 | .PARAMETER Name 1159 | Target computer's host name. 1160 | 1161 | .INPUTS 1162 | This function takes an array of host names or AD computer objects. 1163 | 1164 | .OUTPUTS 1165 | Returns an array of PS Objects with computer Name and IPAddress. 1166 | 1167 | .NOTES 1168 | 1169 | .EXAMPLE 1170 | Get-ComputerIPAddress 1171 | 1172 | Returns a PS Object with the local computer's name and IPv4 address. 1173 | 1174 | .LINK 1175 | By Ben Peterson 1176 | linkedin.com/in/benponline 1177 | github.com/benponline 1178 | twitter.com/benponline 1179 | paypal.me/teknically 1180 | #> 1181 | 1182 | [CmdletBinding()] 1183 | Param( 1184 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 1185 | [Alias('ComputerName')] 1186 | [string]$Name = $env:COMPUTERNAME 1187 | ) 1188 | 1189 | begin{ 1190 | $computerIPList = @() 1191 | } 1192 | 1193 | process{ 1194 | $computerIPList += [PSCustomObject]@{ 1195 | Name = $Name; 1196 | IPAddress = (Resolve-DnsName -Type A -Name $Name).IPAddress 1197 | } 1198 | } 1199 | 1200 | end{ 1201 | return $computerIPList 1202 | } 1203 | } 1204 | 1205 | function Get-ComputerLastBootUpTime{ 1206 | <# 1207 | .SYNOPSIS 1208 | Gets the last time a computer booted up. 1209 | 1210 | .DESCRIPTION 1211 | Gets the name and last time a computer or computers booted up. By default targets localhost. 1212 | 1213 | .PARAMETER Name 1214 | Target computer's host name. 1215 | 1216 | .INPUTS 1217 | Can pipe host names or AD computer objects to function. 1218 | 1219 | .OUTPUTS 1220 | PS object with computer Name and LastBootupTime. 1221 | 1222 | .NOTES 1223 | Compatible with Windows 7 and newer. 1224 | 1225 | .EXAMPLE 1226 | Get-ComputerLastBootupTime 1227 | 1228 | Returns the last time the local host booted up. 1229 | 1230 | .EXAMPLE 1231 | Get-ComputerLastBootupTime -Name "Borg" 1232 | 1233 | Returns the last time the computer "Borg" booted up. 1234 | 1235 | .EXAMPLE 1236 | "Computer1","Computer2" | Get-ComputerLastBootupTime 1237 | 1238 | Returns last bootup time for "Computer1" and "Computer2". 1239 | 1240 | .EXAMPLE 1241 | Get-OUComputer -OrganizationalUnit 'Department X' | Get-ComputerLastBootupTime 1242 | 1243 | Returns the last bootup time of all the computers in the 'Department X' organizational unit. 1244 | 1245 | .LINK 1246 | By Ben Peterson 1247 | linkedin.com/in/benponline 1248 | github.com/benponline 1249 | twitter.com/benponline 1250 | paypal.me/teknically 1251 | #> 1252 | 1253 | [CmdletBinding()] 1254 | Param( 1255 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1256 | [Alias('ComputerName')] 1257 | [string]$Name = $env:COMPUTERNAME 1258 | ) 1259 | 1260 | begin{ 1261 | $lastBootUpTimeList = @() 1262 | } 1263 | 1264 | process{ 1265 | $lastBootUpTimeList += Get-CimInstance -ComputerName $Name -Class Win32_OperatingSystem -Property LastBootUpTime | 1266 | Select-Object -Property @{n='Name';e={$_.pscomputername}},LastBootUpTime 1267 | } 1268 | 1269 | end{ 1270 | return $lastBootUpTimeList 1271 | } 1272 | } 1273 | 1274 | function Get-ComputerLastLogonTime{ 1275 | <# 1276 | .SYNOPSIS 1277 | Gets the last time a computer logged onto the domain. 1278 | 1279 | .DESCRIPTION 1280 | Gets the last time a computer or group of computers logged onto the domain. By default gets the last logon time for the local computer. 1281 | 1282 | .PARAMETER Name 1283 | Target computer. 1284 | 1285 | .INPUTS 1286 | You can pipe computer names and computer AD objects to this function. 1287 | 1288 | .OUTPUTS 1289 | PS objects with computer Name and LastLogonTime. 1290 | 1291 | .NOTES 1292 | None. 1293 | 1294 | .EXAMPLE 1295 | Get-ComputerLastLogonTime -Name "Computer1" 1296 | 1297 | Returns the last time 'Computer1" logged into the domain. 1298 | 1299 | .EXAMPLE 1300 | Get-ADComputer -Filter * | Get-ComputerLastLogonTime 1301 | 1302 | Gets the last time all computers in AD logged onto the domain. 1303 | 1304 | .EXAMPLE 1305 | 'Computer1','Computer2' | Get-ComputerLastLogonTime 1306 | 1307 | Returns the last logon time for 'Computer1' and 'Computer2'. 1308 | 1309 | .LINK 1310 | By Ben Peterson 1311 | linkedin.com/in/benponline 1312 | github.com/benponline 1313 | twitter.com/benponline 1314 | paypal.me/teknically 1315 | #> 1316 | 1317 | [cmdletbinding()] 1318 | param( 1319 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1320 | [string]$Name = $env:ComputerName 1321 | ) 1322 | 1323 | begin{ 1324 | $lastLogonList = @() 1325 | 1326 | #When looking for AD computer LastLogon we need to check all domain controllers because this information is not synced between them. 1327 | $domainControllers = (Get-ADDomainController -Filter *).Name 1328 | } 1329 | 1330 | process{ 1331 | $dcCount = $domainControllers.Count 1332 | 1333 | if($dcCount -eq 1){ 1334 | $lastLogonList += Get-ADComputer $Name | 1335 | Get-ADObject -Properties LastLogon | 1336 | Select-Object -Property @{n="Name";e={$Name}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 1337 | }else{ 1338 | $lastLogonTime = Get-ADComputer $Name -Server $domainControllers[0] | 1339 | Get-ADObject -Properties LastLogon | 1340 | Select-Object -Property @{n="Name";e={$Name}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 1341 | 1342 | for($i = 1; $i -LT $dcCount; $i++){ 1343 | $nextlogonTime = Get-ADComputer $Name -Server $domainControllers[$i] | 1344 | Get-ADObject -Properties LastLogon | 1345 | Select-Object -Property @{n="Name";e={$Name}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 1346 | 1347 | 1348 | if($nextlogonTime.LastLogon -GT $lastLogonTime.LastLogon){ 1349 | $lastLogonTime = $nextlogonTime 1350 | } 1351 | } 1352 | 1353 | $lastLogonList += $lastLogonTime 1354 | } 1355 | } 1356 | 1357 | end{ 1358 | return $lastLogonList 1359 | } 1360 | } 1361 | 1362 | function Get-ComputerMemory{ 1363 | <# 1364 | .SYNOPSIS 1365 | Gets the memory in GB of a computer. 1366 | 1367 | .DESCRIPTION 1368 | Gets the memory in GB of a computer or group of computers. 1369 | 1370 | .PARAMETER Name 1371 | Target comuputer's host name. By defualt it is the local computers. 1372 | 1373 | .INPUTS 1374 | Takes an array of computer names or AD computer objects over the pipeline. 1375 | 1376 | .OUTPUTS 1377 | Returns PS Object/s of the computers passed to it including computer name and memory in GB. 1378 | 1379 | .NOTES 1380 | 1381 | .EXAMPLE 1382 | Get-ComputerMemory -Name 'computer1' 1383 | 1384 | Returns a PS Object with the computer name and memory in GB of the 'Computer1'. 1385 | 1386 | .EXAMPLE 1387 | 'computer1','computer2' | Get-ComputerMemory 1388 | 1389 | Returns a PS Object for each computer containing computer name and memory in GB. 1390 | 1391 | .EXAMPLE 1392 | Get-OUComputer -OrhanizationalUnit 'Department X' | Get-ComputerMemory 1393 | 1394 | Returns a PS Object for each computer in the 'Department X' Active Directory organizational unit containing computer name and memory in GB. 1395 | 1396 | .LINK 1397 | By Ben Peterson 1398 | linkedin.com/in/benponline 1399 | github.com/benponline 1400 | twitter.com/benponline 1401 | paypal.me/teknically 1402 | #> 1403 | 1404 | [CmdletBinding()] 1405 | Param( 1406 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 1407 | [Alias('ComputerName')] 1408 | [string]$Name = $env:COMPUTERNAME 1409 | ) 1410 | 1411 | begin{ 1412 | $computerMemories = @() 1413 | } 1414 | 1415 | process{ 1416 | 1417 | if(Test-Connection -TargetName $Name -Count 1 -Quiet){ 1418 | Write-Verbose -Message "Connecting to $Name." 1419 | 1420 | $computerMemories += [PSCustomObject]@{ 1421 | Name = $Name; 1422 | MemoryGB = [math]::Round(((Get-CimInstance -ComputerName $Name -ClassName Win32_ComputerSystem -Property TotalPhysicalMemory).TotalPhysicalMemory / 1GB),1) 1423 | } 1424 | }else{ 1425 | Write-Verbose -Message "$Name is offline." 1426 | } 1427 | } 1428 | 1429 | end{ 1430 | return $computerMemories 1431 | } 1432 | } 1433 | 1434 | function Get-ComputerModel{ 1435 | <# 1436 | .SYNOPSIS 1437 | Gets the model of a computer. 1438 | 1439 | .DESCRIPTION 1440 | Gets the model of a computer or group of computers. 1441 | 1442 | .PARAMETER Name 1443 | The model of the computer with this name will be returned. By defualt it is the local computers. 1444 | 1445 | .INPUTS 1446 | Takes an array of computer names or AD computer objects over the pipeline. 1447 | 1448 | .OUTPUTS 1449 | Returns PS Object/s of the computers passed to it including computer name and model. 1450 | 1451 | .NOTES 1452 | 1453 | .EXAMPLE 1454 | Get-ComputerModel -Name 'computer1' 1455 | 1456 | Returns a PS Object with the computer name and model of the 'Computer1'. 1457 | 1458 | .EXAMPLE 1459 | 'computer1','computer2' | Get-ComputerModel 1460 | 1461 | Returns a PS Object for each computer containing computer name and model. 1462 | 1463 | .EXAMPLE 1464 | Get-OUComputer -OrhanizationalUnit 'Department X' | Get-ComputerModel 1465 | 1466 | Returns a PS Object for each computer in the 'Department X' Active Directory organizational unit containing computer name and model. 1467 | 1468 | .LINK 1469 | By Ben Peterson 1470 | linkedin.com/in/benponline 1471 | github.com/benponline 1472 | twitter.com/benponline 1473 | paypal.me/teknically 1474 | #> 1475 | 1476 | [CmdletBinding()] 1477 | Param( 1478 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 1479 | [Alias('ComputerName')] 1480 | [string]$Name = $env:COMPUTERNAME 1481 | ) 1482 | 1483 | begin{ 1484 | $computerModels = @() 1485 | } 1486 | 1487 | process{ 1488 | $computerModels += Get-CimInstance -ComputerName $Name -ClassName Win32_ComputerSystem -Property Model | Select-Object -Property @{n='Name';e={$Name}},@{n='Model';e={$_.Model}} 1489 | } 1490 | 1491 | end{ 1492 | return $computerModels 1493 | } 1494 | } 1495 | 1496 | function Get-ComputerOS{ 1497 | <# 1498 | .SYNOPSIS 1499 | Gets the operating system name of a computer. 1500 | 1501 | .DESCRIPTION 1502 | Gets the Windows operating system of the local host or remote computer. Does not return build number or any other detailed info. 1503 | 1504 | .PARAMETER Name 1505 | Name of computer the user wants the operating system of. 1506 | 1507 | .INPUTS 1508 | Accepts pipeline input. Host names and AD computer objects. 1509 | 1510 | .OUTPUTS 1511 | PSObject with computer name and operating system. 1512 | 1513 | .NOTES 1514 | 1515 | .EXAMPLE 1516 | Get-ComputerOS 1517 | 1518 | Returns the local host's operating system. 1519 | 1520 | .EXAMPLE 1521 | Get-ComputerOS -Name Computer1 1522 | 1523 | Returns computer name and operating system. 1524 | 1525 | .EXAMPLE 1526 | "Computer1","Computer2" | Get-ComputerOS 1527 | 1528 | Returns the operating system of "Computer1" and "Computer2". 1529 | 1530 | .LINK 1531 | By Ben Peterson 1532 | linkedin.com/in/benponline 1533 | github.com/benponline 1534 | twitter.com/benponline 1535 | paypal.me/teknically 1536 | #> 1537 | 1538 | [CmdletBinding()] 1539 | Param( 1540 | [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] 1541 | [Alias('ComputerName')] 1542 | [string]$Name = $env:COMPUTERNAME 1543 | ) 1544 | 1545 | begin{ 1546 | $computerOSList = @() 1547 | } 1548 | 1549 | process{ 1550 | if(Test-Connection $Name -Quiet -Count 1){ 1551 | try{ 1552 | $computerOSList += Get-CimInstance -ComputerName $Name -ClassName win32_operatingsystem -ErrorAction "Stop" | Select-Object -Property @{n='Name';e={$_.PSComputerName}},Caption,BuildNumber 1553 | }catch{ 1554 | $computerOSList += Get-WmiObject -ComputerName $Name -Class win32_operatingsystem | Select-Object -Property @{n='Name';e={$_.PSComputerName}},Caption,BuildNumber 1555 | } 1556 | } 1557 | } 1558 | 1559 | end{ 1560 | return $computerOSList 1561 | } 1562 | } 1563 | 1564 | function Get-ComputerPhysicalDiskInformation{ 1565 | <# 1566 | .SYNOPSIS 1567 | Gets information about the physical disks of a computer. 1568 | 1569 | .DESCRIPTION 1570 | Returns the health status of the physical disks of the local computer, remote computer, group of computers, or computers in an organizational unit. 1571 | 1572 | .PARAMETER Name 1573 | Specifies the computer the fuction will gather information from. 1574 | 1575 | .INPUTS 1576 | You can pipe host names or AD computer objects. 1577 | 1578 | .OUTPUTS 1579 | Returns objects with disk info including computer name, friendly name, media type, operational status, health status, and size in GB. 1580 | 1581 | .NOTES 1582 | Only returns information from computers running Windows 10 or Windows Server 2012 or higher. 1583 | 1584 | .EXAMPLE 1585 | Get-ComputerPhysicalDiskInformation 1586 | 1587 | Returns disk health information for the local computer. 1588 | 1589 | .EXAMPLE 1590 | Get-ComputerPhysicalDiskInformation -Name Computer1 1591 | 1592 | Returns disk health information for the computer named Computer1. 1593 | 1594 | .EXAMPLE 1595 | "computer1","computer2" | Get-ComputerPhysicalDiskInformation 1596 | 1597 | Returns physical disk information from "computer1" and "computer2". 1598 | 1599 | .LINK 1600 | By Ben Peterson 1601 | linkedin.com/in/benponline 1602 | github.com/benponline 1603 | twitter.com/benponline 1604 | paypal.me/teknically 1605 | #> 1606 | 1607 | [CmdletBinding()] 1608 | Param( 1609 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1610 | [Alias('ComputerName')] 1611 | [string]$Name = $env:COMPUTERNAME 1612 | ) 1613 | 1614 | begin{ 1615 | $physicalDiskList = @() 1616 | } 1617 | 1618 | process{ 1619 | 1620 | if(Test-Connection $Name -Count 1 -Quiet){ 1621 | $physicalDiskList += Get-PhysicalDisk -CimSession $Name | 1622 | Select-Object -Property @{n="ComputerName";e={$Name}},` 1623 | FriendlyName,` 1624 | MediaType,` 1625 | OperationalStatus,` 1626 | HealthStatus,` 1627 | @{n="SizeGB";e={[math]::Round(($_.Size / 1GB),1)}} 1628 | } 1629 | } 1630 | 1631 | end{ 1632 | return $physicalDiskList 1633 | } 1634 | } 1635 | 1636 | function Get-ComputerProcessor{ 1637 | <# 1638 | .SYNOPSIS 1639 | Gets the processor of a computer. 1640 | 1641 | .DESCRIPTION 1642 | Gets the processor of a computer or group of computers. 1643 | 1644 | .PARAMETER Name 1645 | The processor of the computer with this name will be returned. By defualt it is the local computer. 1646 | 1647 | .INPUTS 1648 | Takes an array of computer names or AD computer objects over the pipeline. 1649 | 1650 | .OUTPUTS 1651 | Returns PS Object/s of the computers passed to it including computer name and processor. 1652 | 1653 | .NOTES 1654 | 1655 | .EXAMPLE 1656 | Get-ComputerProcessor -Name 'computer1' 1657 | 1658 | Returns a PS Object with the computer name and processor of the 'Computer1'. 1659 | 1660 | .EXAMPLE 1661 | 'computer1','computer2' | Get-ComputerProcessor 1662 | 1663 | Returns a PS Object for each computer containing computer name and processor. 1664 | 1665 | .EXAMPLE 1666 | Get-OUComputer -OrganizationalUnit 'Department X' | Get-ComputerProcessor 1667 | 1668 | Returns a PS Object for each computer in the 'Department X' Active Directory organizational unit containing computer name and processor. 1669 | 1670 | .LINK 1671 | By Ben Peterson 1672 | linkedin.com/in/benponline 1673 | github.com/benponline 1674 | twitter.com/benponline 1675 | paypal.me/teknically 1676 | #> 1677 | 1678 | [CmdletBinding()] 1679 | Param( 1680 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 1681 | [Alias('ComputerName')] 1682 | [string]$Name = $env:COMPUTERNAME 1683 | ) 1684 | 1685 | begin{ 1686 | $computerProcessors = @() 1687 | } 1688 | 1689 | process{ 1690 | $computerProcessors += Get-CimInstance -ComputerName $Name -ClassName Win32_Processor -Property Name | Select-Object -Property @{n='Name';e={$Name}},@{n='Processor';e={$_.Name}} 1691 | } 1692 | 1693 | end{ 1694 | return $computerProcessors 1695 | } 1696 | } 1697 | 1698 | function Get-ComputerShareFolder{ 1699 | <# 1700 | .SYNOPSIS 1701 | Gets all of the share folders on a computer. 1702 | 1703 | .DESCRIPTION 1704 | This function returns all of the share folders on a computer or remote computer. 1705 | 1706 | .PARAMETER Name 1707 | Target computer's host name. 1708 | 1709 | .INPUTS 1710 | Can accept AD computer objects from the pipeline. 1711 | 1712 | .OUTPUTS 1713 | PS objects with the computer name, share folder name, path to the folder, and status of the folder. 1714 | 1715 | .NOTES 1716 | Requires administrative privileges to work on local machine. 1717 | 1718 | .EXAMPLE 1719 | Get-ComputerShareFolder -Name 'Computer1' 1720 | 1721 | Returns all of the share folders from 'Computer1'. 1722 | 1723 | .EXAMPLE 1724 | Get-ADComputer -filter * | Get-ComputerShareFolder 1725 | 1726 | Returns the share folders from all computers in AD. 1727 | 1728 | .EXAMPLE 1729 | Get-OUComputer -OrganizationalUnit 'Workstations' | Get-ComputerShareFolder 1730 | 1731 | Returns the share folders from all computers in the 'Workstations' OU. Get-OUComputer 1732 | is a function from the PSSystemAdministrator module. 1733 | 1734 | .LINK 1735 | By Ben Peterson 1736 | linkedin.com/in/benponline 1737 | github.com/benponline 1738 | twitter.com/benponline 1739 | paypal.me/teknically 1740 | #> 1741 | 1742 | [cmdletbinding()] 1743 | param( 1744 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1745 | [Alias('ComputerName')] 1746 | [string]$Name = $env:COMPUTERNAME 1747 | ) 1748 | 1749 | begin{ 1750 | #$computerShareList = @() 1751 | $computerShareList = [System.Collections.Generic.List[psobject]]::new() 1752 | $computers = [System.Collections.Generic.List[string]]::new() 1753 | } 1754 | 1755 | process{ 1756 | $computers.Add($Name) 1757 | } 1758 | 1759 | end{ 1760 | $computers | ForEach-Object -Parallel { 1761 | Get-FileShare -CimSession $_ | 1762 | Select-Object -Property ` 1763 | @{n = "ComputerName"; e = {$_.PSComputerName}}, 1764 | @{n = "Name"; e = {$_.Name}}, 1765 | @{n = "Path"; e = {$_.VolumeRelativePath}}, 1766 | @{n = "Status"; e = {$_.OperationalStatus}} 1767 | } | ForEach-Object { $computerShareList.Add($_) } 1768 | 1769 | return $computerShareList 1770 | } 1771 | } 1772 | 1773 | function Get-ComputerSoftware{ 1774 | <# 1775 | .SYNOPSIS 1776 | Gets all of the installed software on a computer or computers. 1777 | 1778 | .DESCRIPTION 1779 | This function gathers all of the installed software on a computer or group of computers. By default gathers from the local host. 1780 | 1781 | .PARAMETER Name 1782 | Host name of target computer. 1783 | 1784 | .INPUTS 1785 | You can pipe host names or computer AD objects input to this function. 1786 | 1787 | .OUTPUTS 1788 | Returns PS objects containing ComputerName, Name, Version, Installdate, UninstallCommand, and RegPath. 1789 | 1790 | .NOTES 1791 | Compatible with Windows 7 and newer. 1792 | 1793 | Requires remote registry service running on remote machines. 1794 | 1795 | .EXAMPLE 1796 | Get-ComputerSoftware 1797 | 1798 | This cmdlet returns all installed software on the local host. 1799 | 1800 | .EXAMPLE 1801 | Get-ComputerSoftware -ComputerName “Computer” 1802 | 1803 | This cmdlet returns all the software installed on "Computer". 1804 | 1805 | .EXAMPLE 1806 | Get-ADComputer -Filter * | Get-ComputerSoftware 1807 | 1808 | This cmdlet returns the installed software on all computers on the domain. 1809 | 1810 | .LINK 1811 | By Ben Peterson 1812 | linkedin.com/in/benponline 1813 | github.com/benponline 1814 | twitter.com/benponline 1815 | paypal.me/teknically 1816 | 1817 | .LINK 1818 | Based on code from: 1819 | https://community.spiceworks.com/scripts/show/2170-get-a-list-of-installed-software-from-a-remote-computer-fast-as-lightning 1820 | #> 1821 | 1822 | [cmdletbinding()] 1823 | param( 1824 | [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] 1825 | [Alias('ComputerName')] 1826 | [string]$Name = $env:COMPUTERNAME 1827 | ) 1828 | 1829 | begin{ 1830 | $masterKeys = [System.Collections.Generic.List[psobject]]::new() 1831 | $computers = [System.Collections.Generic.List[string]]::new() 1832 | 1833 | $lmKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall","SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 1834 | $lmReg = [Microsoft.Win32.RegistryHive]::LocalMachine 1835 | $cuKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall" 1836 | $cuReg = [Microsoft.Win32.RegistryHive]::CurrentUser 1837 | } 1838 | 1839 | process{ 1840 | $computers.Add($Name) 1841 | } 1842 | 1843 | end{ 1844 | $computers | ForEach-Object -Parallel { 1845 | if((Test-Connection -ComputerName $_ -Count 1 -Quiet)){ 1846 | $remoteLMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($using:lmReg,$_) 1847 | 1848 | foreach($key in $using:lmKeys){ 1849 | $regKey = $remoteLMRegKey.OpenSubkey($key) 1850 | 1851 | foreach ($subName in $regKey.GetSubkeyNames()){ 1852 | 1853 | foreach($sub in $regKey.OpenSubkey($subName)){ 1854 | New-Object PSObject -Property @{ 1855 | "ComputerName" = $_; 1856 | "Name" = $sub.getvalue("displayname"); 1857 | "SystemComponent" = $sub.getvalue("systemcomponent"); 1858 | "ParentKeyName" = $sub.getvalue("parentkeyname"); 1859 | "Version" = $sub.getvalue("DisplayVersion"); 1860 | "UninstallCommand" = $sub.getvalue("UninstallString"); 1861 | "InstallDate" = $sub.getvalue("InstallDate"); 1862 | "RegPath" = $sub.ToString() 1863 | } 1864 | } 1865 | } 1866 | } 1867 | } 1868 | } | ForEach-Object {$masterKeys.Add($_)} 1869 | 1870 | $computers | ForEach-Object -Parallel { 1871 | if(Test-Connection -ComputerName $_ -Count 1 -Quiet){ 1872 | $remoteCURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($using:cuReg,$_) 1873 | 1874 | foreach ($key in $using:cuKeys){ 1875 | $regKey = $remoteCURegKey.OpenSubkey($key) 1876 | 1877 | if($null -ne $regKey){ 1878 | 1879 | foreach($subName in $regKey.getsubkeynames()){ 1880 | 1881 | foreach ($sub in $regKey.opensubkey($subName)){ 1882 | New-Object PSObject -Property @{ 1883 | "ComputerName" = $_; 1884 | "Name" = $sub.getvalue("displayname"); 1885 | "SystemComponent" = $sub.getvalue("systemcomponent"); 1886 | "ParentKeyName" = $sub.getvalue("parentkeyname"); 1887 | "Version" = $sub.getvalue("DisplayVersion"); 1888 | "UninstallCommand" = $sub.getvalue("UninstallString"); 1889 | "InstallDate" = $sub.getvalue("InstallDate"); 1890 | "RegPath" = $sub.ToString() 1891 | } 1892 | } 1893 | } 1894 | } 1895 | } 1896 | } 1897 | } | ForEach-Object {$masterKeys.Add($_)} 1898 | 1899 | $woFilter = {$null -ne $_.name -AND $_.SystemComponent -ne "1" -AND $null -eq $_.ParentKeyName} 1900 | $props = 'ComputerName','Name','Version','Installdate','UninstallCommand','RegPath' 1901 | $masterKeys = $masterKeys | Where-Object $woFilter | Select-Object -Property $props 1902 | return $masterKeys 1903 | } 1904 | } 1905 | 1906 | function Get-ComputerSystemEvent{ 1907 | <# 1908 | .SYNOPSIS 1909 | Gets system events from a computer. 1910 | 1911 | .DESCRIPTION 1912 | Gets system errors from computers. By default returns errors from local computer. Can return errors from remote computer(s). Default number of errors returned is 5, but is adjustable. 1913 | 1914 | .PARAMETER Name 1915 | Specifies which computer to pull errors from. 1916 | 1917 | .PARAMETER Newest 1918 | Specifies the number of most recent errors to be returned. 1919 | 1920 | .INPUTS 1921 | Host names or AD computer objects. 1922 | 1923 | .OUTPUTS 1924 | PS objects for computer system errors with Computer, TimeWritten, EventID, InstanceId, and Message. 1925 | 1926 | .NOTES 1927 | Requires "run as administrator". 1928 | 1929 | Requires "Printer and file sharing", "Network Discovery", and "Remote Registry" to be enabled on computers that are searched. This funtion can take a long time to complete if more than 5 computers are searched. 1930 | 1931 | .EXAMPLE 1932 | Get-ComputerSystemEvent 1933 | 1934 | This cmdlet returns the last 5 system events from localhost. 1935 | 1936 | .EXAMPLE 1937 | Get-ComputerSystemEvent -Name Server -Newest 2 1938 | 1939 | This cmdlet returns the last 2 system events from server. 1940 | 1941 | .EXAMPLE 1942 | "computer1","computer2" | Get-ComputerSystemEvent 1943 | 1944 | This cmdlet returns newest 5 system events from "computer1" and "computer2". 1945 | 1946 | .EXAMPLE 1947 | Get-ADComputer Computer1 | Get-ComputerSystemEvent 1948 | 1949 | This cmdlet returns the last 5 system events from Computer1. 1950 | 1951 | .LINK 1952 | By Ben Peterson 1953 | linkedin.com/in/benponline 1954 | github.com/benponline 1955 | twitter.com/benponline 1956 | paypal.me/teknically 1957 | #> 1958 | 1959 | [CmdletBinding()] 1960 | Param( 1961 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 1962 | [Alias('ComputerName')] 1963 | [string]$Name = "$env:COMPUTERNAME", 1964 | [int]$Newest = 5 1965 | ) 1966 | 1967 | begin{ 1968 | $eventLog = [System.Collections.Generic.List[psobject]]::new() 1969 | $computers = [System.Collections.Generic.List[string]]::new() 1970 | } 1971 | 1972 | process{ 1973 | $computers.Add($Name) 1974 | } 1975 | 1976 | end{ 1977 | $computers | ForEach-Object -Parallel { 1978 | Get-WinEvent -LogName System -ComputerName $_ -MaxEvents $using:Newest | 1979 | Select-Object -Property MachineName,TimeCreated,Id,LevelDisplayName,Message 1980 | } | ForEach-Object { $eventLog.Add($_) } 1981 | 1982 | return $eventLog 1983 | } 1984 | } 1985 | 1986 | function Get-CredentialExportToXML{ 1987 | <# 1988 | .SYNOPSIS 1989 | Gets credentials from the user and exports them to location provided by the user. 1990 | 1991 | .DESCRIPTION 1992 | This function promps the user for a user name and password. It encrypts the password and saves it all to an XML file at the path provided to the function. You can then import these credentials in other functions and scripts that require credentials without having to hard code them in. 1993 | 1994 | .PARAMETER FileName 1995 | The name that will be given to the file containing the credentials. Do not include file extention. 1996 | 1997 | .PARAMETER Path 1998 | The directory the credentials will be saved. Do not include trailing "\". 1999 | 2000 | .INPUTS 2001 | None. 2002 | 2003 | .OUTPUTS 2004 | XML file with credentials. 2005 | 2006 | .NOTES 2007 | 2008 | .EXAMPLE 2009 | Get-CredentialExportToXML -FileName Creds -Path C:\ScriptCreds 2010 | 2011 | Promps user for user name and password. Encryps the password and saves the credentials at C:\ScriptCreds\Creds.clixml 2012 | 2013 | .LINK 2014 | By Ben Peterson 2015 | linkedin.com/in/benponline 2016 | github.com/benponline 2017 | twitter.com/benponline 2018 | paypal.me/teknically 2019 | #> 2020 | 2021 | [cmdletbinding()] 2022 | param( 2023 | [Parameter(Mandatory=$true)] 2024 | [string]$FileName, 2025 | 2026 | [Parameter(Mandatory=$true)] 2027 | [string]$Path 2028 | ) 2029 | 2030 | $credential = Get-Credential 2031 | Export-Clixml -Path "$Path\$FileName.xml" -InputObject $credential 2032 | } 2033 | 2034 | function Get-DHCPReservation{ 2035 | <# 2036 | .SYNOPSIS 2037 | Gets all reservations for a computer in DHCP. 2038 | 2039 | .DESCRIPTION 2040 | This function returns all IPv4 reservations or the reservations for a specific computer from all DHCP servers. 2041 | 2042 | .PARAMETER ComputerName 2043 | Host name of the computer in the reservation that will be returned. 2044 | 2045 | .INPUTS 2046 | None. 2047 | 2048 | .OUTPUTS 2049 | None. 2050 | 2051 | .NOTES 2052 | This function is meant to be used in a domain with one DHCP scope. 2053 | 2054 | .EXAMPLE 2055 | Get-DHCPReservation -ComputerName "Computer1" 2056 | 2057 | Gets any reservations for "Computer1" in all available DHCP servers in the domain. 2058 | 2059 | .LINK 2060 | By Ben Peterson 2061 | linkedin.com/in/benponline 2062 | github.com/benponline 2063 | twitter.com/benponline 2064 | paypal.me/teknically 2065 | #> 2066 | 2067 | [cmdletbinding()] 2068 | param( 2069 | [Parameter()] 2070 | [string]$ComputerName = "" 2071 | ) 2072 | 2073 | $dhcpServers = (Get-DhcpServerInDC).DnsName 2074 | $output = @() 2075 | 2076 | if($ComputerName -EQ ""){ 2077 | $hostName = ""; 2078 | }else{ 2079 | $hostName = (Get-ADComputer -Identity $ComputerName).DNSHostName 2080 | } 2081 | 2082 | foreach($server in $dhcpServers){ 2083 | $dhcpServer = $server.split(".")[0] 2084 | $scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId 2085 | $reservations = Get-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer 2086 | 2087 | foreach($r in $reservations){ 2088 | $r | Add-Member -MemberType NoteProperty -Name "DHCPServer" -Value $server 2089 | $output += $r 2090 | } 2091 | } 2092 | 2093 | if($hostName -NE ""){ 2094 | $output = $output | Where-Object -Property Name -EQ $hostName 2095 | } 2096 | 2097 | return $output | Select-Object -Property DHCPServer,AddressState,ClientId,Description,IPAddress,Name,ScopeId,Type 2098 | } 2099 | 2100 | function Get-DirectorySize{ 2101 | <# 2102 | .SYNOPSIS 2103 | Gets the size of a directory. 2104 | 2105 | .DESCRIPTION 2106 | Gets the size of a directory or directories in GB. 2107 | 2108 | .PARAMETER Path 2109 | Path to the directory to be measured. 2110 | 2111 | .INPUTS 2112 | None. 2113 | 2114 | .OUTPUTS 2115 | Returns object with directory and size in GB. 2116 | 2117 | .NOTES 2118 | Command needs to be run as an administrator to ensure all files are checked. 2119 | 2120 | .EXAMPLE 2121 | Get-DirectorySize -Path 'C:\Users' 2122 | 2123 | Returns the size of the Users folder. 2124 | 2125 | .EXAMPLE 2126 | '\\FileShareServer\Folder1','\\FileShareServer\Folder2' | Get-DirectorySize 2127 | 2128 | .LINK 2129 | By Ben Peterson 2130 | linkedin.com/in/benponline 2131 | github.com/benponline 2132 | twitter.com/benponline 2133 | paypal.me/teknically 2134 | 2135 | .Link 2136 | Based on: https://www.gngrninja.com/script-ninja/2016/5/24/powershell-calculating-folder-sizes 2137 | #> 2138 | 2139 | [CmdletBinding()] 2140 | Param( 2141 | [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 2142 | [Alias('Directory')] 2143 | [string] $Path 2144 | ) 2145 | 2146 | begin{ 2147 | $directorySizes = @() 2148 | } 2149 | 2150 | process{ 2151 | $directorySize = (Get-ChildItem -Path $Path -File -Recurse -Force | Measure-Object -Sum Length).sum 2152 | 2153 | $directorySizes += [PSCustomObject]@{ 2154 | Directory = $Path; 2155 | SizeGB = [math]::round(($directorySize / 1GB),2) 2156 | } 2157 | } 2158 | 2159 | end{ 2160 | return $directorySizes 2161 | } 2162 | } 2163 | 2164 | function Get-DisabledComputer{ 2165 | <# 2166 | .SYNOPSIS 2167 | Gets a list of all computers that are disabled. 2168 | 2169 | .DESCRIPTION 2170 | Gets a list of computers from AD that are disabled with information including name, enabled status, DNSHostName, and DistinguishedName. 2171 | 2172 | .PARAMETER OrganizationalUnit 2173 | Focuses the function on a specific AD organizational unit. 2174 | 2175 | .INPUTS 2176 | None. 2177 | 2178 | .OUTPUTS 2179 | PS objects with information including Name, Enabled status, DNSHostName, and DistinguishedName. 2180 | 2181 | .NOTES 2182 | 2183 | .EXAMPLE 2184 | Get-DisabledComputer 2185 | 2186 | Returns a list of all AD computers that are currently disabled. 2187 | 2188 | .EXAMPLE 2189 | Get-DisabledComputer -OrganizationalUnit "Servers" 2190 | 2191 | Returns a list of all AD computers in the organizational unit "Servers" that are currently disabled. 2192 | 2193 | 2194 | .LINK 2195 | By Ben Peterson 2196 | linkedin.com/in/benponline 2197 | github.com/benponline 2198 | twitter.com/benponline 2199 | paypal.me/teknically 2200 | #> 2201 | 2202 | [CmdletBinding()] 2203 | Param( 2204 | [string]$OrganizationalUnit = "" 2205 | ) 2206 | 2207 | if($OrganizationalUnit -eq ""){ 2208 | $disabledComputers = Get-ADComputer -Filter * | Where-Object -Property Enabled -EQ $False 2209 | }else{ 2210 | $disabledComputers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit | 2211 | Where-Object -Property Enabled -EQ $False 2212 | } 2213 | 2214 | return $disabledComputers 2215 | } 2216 | 2217 | function Get-DisabledUser{ 2218 | <# 2219 | .SYNOPSIS 2220 | Gets a list of all users that are disabled. 2221 | 2222 | .DESCRIPTION 2223 | Returns a list of users from AD that are disabled with information including name, enabled, and user principal name. Function can be limited in scope to a specific organizational unit. 2224 | 2225 | .PARAMETER OrganizationalUnit 2226 | Focuses the function on a specific AD organizational unit. 2227 | 2228 | .INPUTS 2229 | None. 2230 | 2231 | .OUTPUTS 2232 | PS objects with information including name, DNSHostName, and DistinguishedName. 2233 | 2234 | .NOTES 2235 | 2236 | .EXAMPLE 2237 | Get-DisabledUser 2238 | 2239 | Returns a list of all AD users that are currently disabled. 2240 | 2241 | .EXAMPLE 2242 | Get-DisabledUser -OrganizationalUnit "Employees" 2243 | 2244 | Returns a list of all AD users that are currently disabled in the "Employees" organizational unit. 2245 | 2246 | .LINK 2247 | By Ben Peterson 2248 | linkedin.com/in/benponline 2249 | github.com/benponline 2250 | twitter.com/benponline 2251 | paypal.me/teknically 2252 | #> 2253 | 2254 | [CmdletBinding()] 2255 | Param( 2256 | [string]$OrganizationalUnit = "" 2257 | ) 2258 | 2259 | if($OrganizationalUnit -eq ""){ 2260 | $disabledUsers = Get-ADUser -Filter * | Where-Object -Property Enabled -EQ $False 2261 | }else{ 2262 | $disabledUsers = Get-OUUser -OrganizationalUnit $OrganizationalUnit | Where-Object -Property Enabled -EQ $False 2263 | } 2264 | 2265 | return $disabledUsers 2266 | } 2267 | 2268 | function Get-InactiveComputer{ 2269 | <# 2270 | .SYNOPSIS 2271 | Gets computers that have not logged onto the domain for more than 30 days. 2272 | 2273 | .DESCRIPTION 2274 | Gets computers from Active Directory that have not logged onto the domain for more than 30 days. The default amount of days is 30. Can be limited to a specific organizational unit. 2275 | 2276 | .PARAMETER Days 2277 | Sets minimum age for last logon time. 2278 | 2279 | .PARAMETER OrganizationalUnit 2280 | Focuses the function on a specific organizational unit. 2281 | 2282 | .INPUTS 2283 | None. 2284 | 2285 | .OUTPUTS 2286 | PS objects with information including computer Name and LastLogonTime. 2287 | 2288 | .NOTES 2289 | 2290 | .EXAMPLE 2291 | Get-InactiveComputer 2292 | 2293 | Lists all computers in the domain that have not been online for more than 30 days. 2294 | 2295 | .EXAMPLE 2296 | Get-InactiveComputer -Days 35 -OrganizationalUnit 'Department X' 2297 | 2298 | Lists all computers in the 'Department X' organizational unit that have not been on the network for 35 days. 2299 | 2300 | .LINK 2301 | By Ben Peterson 2302 | linkedin.com/in/benponline 2303 | github.com/benponline 2304 | twitter.com/benponline 2305 | paypal.me/teknically 2306 | #> 2307 | 2308 | [CmdletBinding()] 2309 | Param( 2310 | [int]$Days = 30, 2311 | [string]$OrganizationalUnit = "" 2312 | ) 2313 | 2314 | if($OrganizationalUnit -eq ""){ 2315 | $computers = Get-ADComputer -Filter * | 2316 | Get-ComputerLastLogonTime | 2317 | Where-Object -Property LastLogon -LT ((Get-Date).AddDays(($Days * -1))) 2318 | }else{ 2319 | $computers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit | 2320 | Get-ComputerLastLogonTime | 2321 | Where-Object -Property LastLogon -LT ((Get-Date).AddDays(($Days * -1))) 2322 | } 2323 | 2324 | return $computers 2325 | } 2326 | 2327 | function Get-InactiveFile{ 2328 | <# 2329 | .SYNOPSIS 2330 | Gets all files in a directory that have not been accessed in the last 24 hours. 2331 | 2332 | .DESCRIPTION 2333 | Gets all files in a directory recursively that have not been access recently. Returns file name, last access time, size in MB, and full name. 2334 | 2335 | .PARAMETER Path 2336 | Function will gather all files recursively from this directory. 2337 | 2338 | .PARAMETER Days 2339 | Function will return only files that have not been accessed for over this many days. By default is set to 1 and function returns all files. 2340 | 2341 | .INPUTS 2342 | You can pipe multiple paths to this function. 2343 | 2344 | .OUTPUTS 2345 | Array of PS objects that includes file names, last access time, size in MB, and full name. 2346 | 2347 | .NOTES 2348 | 2349 | .EXAMPLE 2350 | Get-InactiveFiles -Path C:\Directory1 -DaysInactive 5 2351 | 2352 | Gathers all files recursively in the "Directory1" folder that have not been accessed in over 5 days. 2353 | 2354 | .EXAMPLE 2355 | "C:\Directory1","C:\Directory2" | Get-InactiveFiles 2356 | 2357 | Gathers all files recursively in the "Directory1" and "Directory2" folders. 2358 | 2359 | .LINK 2360 | By Ben Peterson 2361 | linkedin.com/in/benponline 2362 | github.com/benponline 2363 | twitter.com/benponline 2364 | paypal.me/teknically 2365 | #> 2366 | 2367 | [cmdletbinding()] 2368 | param( 2369 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 2370 | [Alias('FullName')] 2371 | [string]$Path, 2372 | [int]$Days = 1 2373 | ) 2374 | 2375 | begin{ 2376 | $files = @() 2377 | $fileAge = (Get-Date).AddDays(-1 * $Days) 2378 | } 2379 | 2380 | process{ 2381 | $files += Get-ChildItemLastWriteTime -Path $Path | 2382 | Where-Object -Property LastWriteTime -LT $fileAge 2383 | } 2384 | 2385 | end{ 2386 | return $files 2387 | } 2388 | } 2389 | 2390 | function Get-InactiveUser{ 2391 | <# 2392 | .SYNOPSIS 2393 | Gets a list of all users that have not logged on to the domain for 30 days. 2394 | 2395 | .DESCRIPTION 2396 | Gets a list of users in active directory that have been inactive for a number of days. The default number of days is 30. Function can also be focused on a specific OU. 2397 | 2398 | .PARAMETER Days 2399 | Determines how long the user account has to be inactive for it to be returned. 2400 | 2401 | .PARAMETER OrganizationalUnit 2402 | Focuses the function on a specific AD organizational unit. 2403 | 2404 | .INPUTS 2405 | None. 2406 | 2407 | .OUTPUTS 2408 | PS objects with SamAccountName and LastLogonTime. 2409 | 2410 | .NOTES 2411 | 2412 | .EXAMPLE 2413 | Get-InactiveUser 2414 | 2415 | Lists all users in the domain that have not checked in for more than 30 days. 2416 | 2417 | .EXAMPLE 2418 | Get-InactiveUser -Days 2 2419 | 2420 | Lists all users in the domain that have not checked in for more than 2 days. 2421 | 2422 | .EXAMPLE 2423 | Get-InactiveUser -Days 45 -OrganizationalUnit "Department X Users" 2424 | 2425 | Lists all users in the domain that have not checked in for more than 45 days in the "Department X Users" organizational unit. 2426 | 2427 | .LINK 2428 | By Ben Peterson 2429 | linkedin.com/in/benponline 2430 | github.com/benponline 2431 | twitter.com/benponline 2432 | paypal.me/teknically 2433 | #> 2434 | 2435 | [CmdletBinding()] 2436 | Param( 2437 | [int]$Days = 30, 2438 | [string]$OrganizationalUnit = "" 2439 | ) 2440 | 2441 | if($OrganizationalUnit -eq ""){ 2442 | $users = Get-ADUser -Filter * | 2443 | Get-UserLastLogonTime | 2444 | Where-Object -Property LastLogon -LT ((Get-Date).AddDays($Days * -1)) 2445 | }else{ 2446 | $users = Get-OUUser -OrganizationalUnit $OrganizationalUnit | 2447 | Get-UserLastLogonTime | 2448 | Where-Object -Property LastLogon -LT ((Get-Date).AddDays($Days * -1)) 2449 | } 2450 | 2451 | return $users 2452 | } 2453 | 2454 | function Get-ItemLastAccessTime{ 2455 | <# 2456 | .SYNOPSIS 2457 | Gets the last access time from an item. 2458 | 2459 | .DESCRIPTION 2460 | Gets an item's file name, last access time, size in MB, and full name. 2461 | 2462 | .PARAMETER Path 2463 | Full path to item. 2464 | 2465 | .INPUTS 2466 | You can pipe multiple paths to this function. 2467 | 2468 | .OUTPUTS 2469 | Array of PS objects that includes FileNames, LastAccessTime, SizeMB, and FullName. 2470 | 2471 | .NOTES 2472 | 2473 | .EXAMPLE 2474 | Get-ItemLastAccessTime -Path "C:\Directory1\file.txt" 2475 | 2476 | Gets information on file.txt. 2477 | 2478 | .EXAMPLE 2479 | "C:\Directory1\file.txt","C:\Directory2\file2.txt" | Get-ItemLastAccessTime 2480 | 2481 | Gets information on file.txt and file2.txt. 2482 | 2483 | .LINK 2484 | By Ben Peterson 2485 | linkedin.com/in/benponline 2486 | github.com/benponline 2487 | twitter.com/benponline 2488 | paypal.me/teknically 2489 | #> 2490 | 2491 | [cmdletbinding()] 2492 | param( 2493 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 2494 | [string[]]$Path 2495 | ) 2496 | 2497 | begin{ 2498 | $paths = @() 2499 | $items = @() 2500 | } 2501 | 2502 | process{ 2503 | foreach($p in $Path){ 2504 | $paths += $p 2505 | } 2506 | } 2507 | 2508 | end{ 2509 | foreach($p in $paths){ 2510 | $items += Get-Item -Path $p | Select-Object -Property Name,LastAccessTime,@{n='SizeMB';e={[math]::Round(($_.Length/1MB),3)}},FullName 2511 | } 2512 | 2513 | return $items 2514 | } 2515 | } 2516 | 2517 | function Get-ItemLastWriteTime{ 2518 | <# 2519 | .SYNOPSIS 2520 | Gets the last write time from an item. 2521 | 2522 | .DESCRIPTION 2523 | Gets an item's file name, last write time, size in MB, and full name. 2524 | 2525 | .PARAMETER Path 2526 | Full path to item. 2527 | 2528 | .INPUTS 2529 | You can pipe multiple paths to this function. 2530 | 2531 | .OUTPUTS 2532 | Array of PS objects that includes FileNames, LastWriteTime, SizeMB, and FullName. 2533 | 2534 | .NOTES 2535 | 2536 | .EXAMPLE 2537 | Get-ItemLastWriteTime -Path "C:\Directory1\file.txt" 2538 | 2539 | Gets information on file.txt. 2540 | 2541 | .EXAMPLE 2542 | "C:\Directory1\file.txt","C:\Directory2\file2.txt" | Get-ItemLastWriteTime 2543 | 2544 | Gets information on file.txt and file2.txt. 2545 | 2546 | .LINK 2547 | By Ben Peterson 2548 | linkedin.com/in/benponline 2549 | github.com/benponline 2550 | twitter.com/benponline 2551 | paypal.me/teknically 2552 | #> 2553 | 2554 | [cmdletbinding()] 2555 | param( 2556 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 2557 | [string[]]$Path 2558 | ) 2559 | 2560 | begin{ 2561 | $items = @() 2562 | $paths = @() 2563 | } 2564 | 2565 | process{ 2566 | foreach($p in $Path){ 2567 | $paths += $p 2568 | } 2569 | } 2570 | 2571 | end{ 2572 | foreach($p in $paths){ 2573 | $items += Get-ChildItem -Path $p | Select-Object -Property Name,LastWriteTime,@{n='SizeMB';e={[math]::Round(($_.Length/1MB),3)}},FullName 2574 | } 2575 | 2576 | return $items 2577 | } 2578 | } 2579 | 2580 | function Get-LargeFile{ 2581 | <# 2582 | .SYNOPSIS 2583 | Gets files larger than 500 MB from a directory. 2584 | 2585 | .DESCRIPTION 2586 | Gets files from a directory recursively that are larger than 500 MB. Directory and file size can be set. 2587 | 2588 | .PARAMETER Path 2589 | Sets the directory the function searches. 2590 | 2591 | .PARAMETER Megabytes 2592 | Sets the file size minimum for files that are returned. 2593 | 2594 | .INPUTS 2595 | Directories can be piped to this function. 2596 | 2597 | .OUTPUTS 2598 | PS objects with name, fileSizeMB, and full name. 2599 | 2600 | .NOTES 2601 | 2602 | .EXAMPLE 2603 | Get-LargeFiles -Path "C:\Folder1" -FileSizeMB 1000 2604 | 2605 | Returns all files over 1000MB from the "folder1" directory. 2606 | 2607 | .EXAMPLE 2608 | "C:\Folder1","C:\Folder2" | Get-LargeFiles -FileSizeMB 5000 2609 | 2610 | Returns all files over 5000MB from the "folder1" and "folder2" directories. 2611 | 2612 | .LINK 2613 | By Ben Peterson 2614 | linkedin.com/in/benponline 2615 | github.com/benponline 2616 | twitter.com/benponline 2617 | paypal.me/teknically 2618 | #> 2619 | 2620 | [cmdletbinding()] 2621 | param( 2622 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 2623 | [Alias('Directory','FullName')] 2624 | [string]$Path, 2625 | [int]$Megabytes = 500 2626 | ) 2627 | 2628 | begin{ 2629 | $largeFiles = @() 2630 | } 2631 | 2632 | process{ 2633 | $largeFiles += Get-ChildItem -Path $Path -File -Recurse -Force | Where-Object -Property Length -GT ($Megabytes * 1000000) 2634 | } 2635 | 2636 | end{ 2637 | $largeFiles = $largeFiles | Select-Object -Property Name,@{n="SizeMB";e={[math]::round(($_.Length / 1MB),1)}},FullName 2638 | return $largeFiles 2639 | } 2640 | } 2641 | 2642 | function Get-LockedOutUser{ 2643 | <# 2644 | .SYNOPSIS 2645 | Gets locked out users from Active Directory. 2646 | 2647 | .DESCRIPTION 2648 | Gets all locked users accounts in Active Directory. Function can be limited to a single organizational unit. 2649 | 2650 | .PARAMETER OrganizationalUnit 2651 | Only returns user AD objects from this organizational unit. 2652 | 2653 | .INPUTS 2654 | None. 2655 | 2656 | .OUTPUTS 2657 | User AD objects. 2658 | 2659 | .NOTES 2660 | 2661 | .EXAMPLE 2662 | Get-LockedOutUser 2663 | 2664 | Gets all locked out users in AD. 2665 | 2666 | .EXAMPLE 2667 | Get-LockedOutUser -OrganizationalUnit 'Department X Users' 2668 | 2669 | Gets all locked out users in the 'Department X Users' organizational unit. 2670 | 2671 | .LINK 2672 | By Ben Peterson 2673 | linkedin.com/in/benponline 2674 | github.com/benponline 2675 | twitter.com/benponline 2676 | paypal.me/teknically 2677 | Used information from: https://social.technet.microsoft.com/wiki/contents/articles/52327.windows-track-down-an-account-lockout-source-and-the-reason-with-powershell.aspx 2678 | #> 2679 | 2680 | [CmdletBinding()] 2681 | Param( 2682 | [string]$OrganizationalUnit = "" 2683 | ) 2684 | 2685 | $lockedOutUsers = @() 2686 | $lockedOutUsersFiltered = @() 2687 | $lockedOutUsersRaw = Search-ADAccount -LockedOut -UsersOnly 2688 | 2689 | if($OrganizationalUnit -ne ""){ 2690 | $ouUsers = Get-OUUser -OrganizationalUnit $OrganizationalUnit 2691 | 2692 | foreach($user in $ouUsers){ 2693 | foreach($lockedOutUser in $lockedOutUsersRaw){ 2694 | if($user.SamAccountName -eq $lockedOutUser.SamAccountName){ 2695 | $lockedOutUsersFiltered += $lockedOutUser 2696 | } 2697 | } 2698 | } 2699 | }else{ 2700 | $lockedOutUsersFiltered = $lockedOutUsersRaw 2701 | } 2702 | 2703 | $lockedOutUsers = $lockedOutUsersFiltered 2704 | 2705 | return $lockedOutUsers 2706 | } 2707 | 2708 | function Get-LockedOutUserEvent{ 2709 | <# 2710 | .SYNOPSIS 2711 | Gets events about user accounts getting locked in Active Directory. 2712 | 2713 | .DESCRIPTION 2714 | Gets events about user account getting locked in Active Directory from all domain contollers. 2715 | 2716 | .PARAMETER OrganizationalUnit 2717 | Only gets lockout events for users in this OU. 2718 | 2719 | .INPUTS 2720 | None. 2721 | 2722 | .OUTPUTS 2723 | PowerShell objects with the following properties: 2724 | TimeCreated - Date time the event was recorded 2725 | Id - Event ID 2726 | User - SamAccountName 2727 | Source - Computer where the lockout occured 2728 | DomainController - Domain controller that recorded the event 2729 | Domain - Domain that the user belongs to. Can be a domain or local machine 2730 | 2731 | .NOTES 2732 | There my be duplicate results returned if the event has been recorded on multiple domain controllers. 2733 | 2734 | .EXAMPLE 2735 | Get-LockedOutUserEvent 2736 | 2737 | Gets all events for user account lockouts from all domain controllers. 2738 | 2739 | .EXAMPLE 2740 | Get-LockedOutUserEvent -OrganizationalUnit 'Department X Users' 2741 | 2742 | Gets all events for user account lockouts for users in the 'Department X Users' OU from all domain controllers. 2743 | 2744 | .LINK 2745 | By Ben Peterson 2746 | linkedin.com/in/benponline 2747 | github.com/benponline 2748 | twitter.com/benponline 2749 | paypal.me/teknically 2750 | https://social.technet.microsoft.com/wiki/contents/articles/52327.windows-track-down-an-account-lockout-source-and-the-reason-with-powershell.aspx 2751 | #> 2752 | 2753 | [CmdletBinding()] 2754 | Param( 2755 | [string]$OrganizationalUnit = "", 2756 | [int]$DaysBack = 1 2757 | ) 2758 | 2759 | $lockedOutUsers = @() 2760 | $lockedOutUsersRaw = @() 2761 | $lockedOutUsersFiltered = @() 2762 | $domainControllers = (Get-ADDomainController -Filter *).Name 2763 | $eventAge = (Get-Date).AddDays(-1 * $DaysBack) 2764 | 2765 | if($OrganizationalUnit -eq ""){ 2766 | foreach ($dc in $domainControllers){ 2767 | $lockedOutUsersFiltered += Get-WinEvent -ComputerName $dc -FilterHashtable @{LogName = 'Security'; ID = 4740} | 2768 | Where-Object -Property TimeCreated -GT $eventAge 2769 | } 2770 | }else{ 2771 | foreach ($dc in $domainControllers){ 2772 | $lockedOutUsersRaw += Get-WinEvent -ComputerName $dc -FilterHashtable @{LogName = 'Security'; ID = 4740} | 2773 | Where-Object -Property TimeCreated -GT $eventAge 2774 | } 2775 | 2776 | $ouUsers = Get-OUUser -OrganizationalUnit $OrganizationalUnit 2777 | 2778 | foreach ($user in $ouUsers){ 2779 | foreach($lockedOutUser in $lockedOutUsersRaw){ 2780 | if($user.SamAccountName -eq $lockedOutUser.Properties[0].Value){ 2781 | $lockedOutUsersFiltered += $lockedOutUser 2782 | } 2783 | } 2784 | } 2785 | } 2786 | 2787 | foreach($user in $lockedOutUsersFiltered){ 2788 | $lockedOutUsers += [PSCustomObject]@{ 2789 | TimeCreated = $user.TimeCreated 2790 | Id = $user.Id 2791 | User = $user.Properties[0].Value 2792 | Source = $user.Properties[1].Value 2793 | DomainController = $user.Properties[4].Value 2794 | Domain = $user.Properties[5].Value 2795 | } 2796 | } 2797 | 2798 | return $lockedOutUsers 2799 | } 2800 | 2801 | function Get-OfflineComputer{ 2802 | <# 2803 | .SYNOPSIS 2804 | Gets computers that are offline. 2805 | 2806 | .DESCRIPTION 2807 | Gets computers that are offline with information including name, DNS host name, and distinguished name. 2808 | 2809 | .PARAMETER OrganizationalUnit 2810 | Focuses the function on a specific AD organizational unit. 2811 | 2812 | .PARAMETER PingTimeoutMS 2813 | The time in milliseconds to wait for each server to respond to the ping. Default is 100. 2814 | 2815 | .INPUTS 2816 | None. 2817 | 2818 | .OUTPUTS 2819 | PS objects containing Name, DNSHostName, and DistinguishedName. 2820 | 2821 | .NOTES 2822 | Firewalls must be configured to allow ping requests. 2823 | 2824 | .EXAMPLE 2825 | Get-OfflineComputer 2826 | 2827 | Returns list of all AD computers that are currently offline. 2828 | 2829 | .EXAMPLE 2830 | Get-OfflineComputer -OrganizationalUnit 'Department X' 2831 | 2832 | Returns the offline computers from the 'Department X' organizational unit. 2833 | 2834 | .LINK 2835 | By Ben Peterson 2836 | linkedin.com/in/benponline 2837 | github.com/benponline 2838 | twitter.com/benponline 2839 | paypal.me/teknically 2840 | #> 2841 | 2842 | [CmdletBinding()] 2843 | Param ( 2844 | [string]$OrganizationalUnit = "", 2845 | [int]$PingTimeoutMS = 100 2846 | ) 2847 | 2848 | begin { 2849 | if ($OrganizationalUnit -eq "") { 2850 | $computers = Get-ADComputer -Filter * 2851 | } else { 2852 | $computers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit 2853 | } 2854 | $pingTasks = New-Object 'System.Collections.Generic.Dictionary[string, System.Threading.Tasks.Task]' 2855 | } 2856 | 2857 | process { 2858 | foreach ($computer in $computers) { 2859 | $ping = New-Object -TypeName 'System.Net.NetworkInformation.Ping' 2860 | $pingTasks.Add($computer.Name, $ping.SendPingAsync($computer.Name, $PingTimeoutMS)) 2861 | } 2862 | } 2863 | 2864 | end { 2865 | while ($pingTasks.Values.IsCompleted -contains $false) { 2866 | Start-Sleep -Milliseconds $PingTimeoutMS 2867 | } 2868 | 2869 | $computers | Where-Object { 2870 | $pingTasks[$_.Name].Result.Status -ne 0 2871 | } 2872 | } 2873 | } 2874 | 2875 | function Get-OnlineComputer{ 2876 | <# 2877 | .SYNOPSIS 2878 | Gets computers that are online. 2879 | 2880 | .DESCRIPTION 2881 | Gets computers that are online with information including name, DNS host name, and distinguished name. 2882 | 2883 | .PARAMETER OrganizationalUnit 2884 | Focuses the function on a specific AD organizational unit. 2885 | 2886 | .PARAMETER PingTimeoutMS 2887 | The time in milliseconds to wait for each server to respond to the ping. Default is 100. 2888 | 2889 | .INPUTS 2890 | None. 2891 | 2892 | .OUTPUTS 2893 | PS objects containing Name, DNSHostName, and DistinguishedName. 2894 | 2895 | .NOTES 2896 | Firewalls must be configured to allow ping requests. 2897 | 2898 | .EXAMPLE 2899 | Get-OnlineComputer 2900 | 2901 | Returns list of all AD computers that are currently online. 2902 | 2903 | .EXAMPLE 2904 | Get-OnlineComputer -OrganizationalUnit 'Department X' 2905 | 2906 | Returns the online computers from the 'Department X' organizational unit. 2907 | 2908 | .LINK 2909 | By Ben Peterson 2910 | linkedin.com/in/benponline 2911 | github.com/benponline 2912 | twitter.com/benponline 2913 | paypal.me/teknically 2914 | #> 2915 | 2916 | [CmdletBinding()] 2917 | Param ( 2918 | [string]$OrganizationalUnit = "", 2919 | [int]$PingTimeoutMS = 100 2920 | ) 2921 | 2922 | begin { 2923 | if ($OrganizationalUnit -eq "") { 2924 | $computers = Get-ADComputer -Filter * 2925 | } else { 2926 | $computers = Get-OUComputer -OrganizationalUnit $OrganizationalUnit 2927 | } 2928 | $pingTasks = New-Object 'System.Collections.Generic.Dictionary[string, System.Threading.Tasks.Task]' 2929 | } 2930 | 2931 | process { 2932 | foreach ($computer in $computers) { 2933 | $ping = New-Object -TypeName 'System.Net.NetworkInformation.Ping' 2934 | $pingTasks.Add($computer.Name, $ping.SendPingAsync($computer.Name, $PingTimeoutMS)) 2935 | } 2936 | } 2937 | 2938 | end { 2939 | while ($pingTasks.Values.IsCompleted -contains $false) { 2940 | Start-Sleep -Milliseconds $PingTimeoutMS 2941 | } 2942 | 2943 | $computers | Where-Object { 2944 | $pingTasks[$_.Name].Result.Status -eq 0 2945 | } 2946 | } 2947 | } 2948 | 2949 | function Get-OUComputer{ 2950 | <# 2951 | .SYNOPSIS 2952 | Gets computers from a specific organizational unit. 2953 | 2954 | .DESCRIPTION 2955 | Gets computer AD objects for each computer in an AD organizaitional unit. 2956 | 2957 | .PARAMETER OrganizationalUnit 2958 | Sets the scope of the function to the provided organizational unit. 2959 | 2960 | .INPUTS 2961 | None. 2962 | 2963 | .OUTPUTS 2964 | Array of AD computer objects. 2965 | 2966 | .NOTES 2967 | 2968 | .EXAMPLE 2969 | Get-OUComputer -OrganizationalUnit "Workstations" 2970 | 2971 | Returns AD objects for all computers in the Workstations organizational unit. 2972 | 2973 | .LINK 2974 | By Ben Peterson 2975 | linkedin.com/in/benponline 2976 | github.com/benponline 2977 | twitter.com/benponline 2978 | paypal.me/teknically 2979 | #> 2980 | 2981 | [cmdletbinding()] 2982 | param( 2983 | [parameter(ValueFromPipeline=$True,Mandatory=$True)] 2984 | [string]$OrganizationalUnit 2985 | ) 2986 | 2987 | begin{ 2988 | $domainInfo = (Get-ADDomain).DistinguishedName 2989 | $computers = @() 2990 | } 2991 | 2992 | process{ 2993 | $computers += Get-ADComputer -Filter * -SearchBase "ou=$OrganizationalUnit,$domainInfo" 2994 | } 2995 | 2996 | end{ 2997 | return $computers 2998 | } 2999 | } 3000 | 3001 | function Get-OUUser{ 3002 | <# 3003 | .SYNOPSIS 3004 | Gets users from a specific organizational unit. 3005 | 3006 | .DESCRIPTION 3007 | Gets user AD objects for each user in an AD organizaitional unit. 3008 | 3009 | .PARAMETER OrganizationalUnit 3010 | Sets the scope of the function to the provided organizational unit. 3011 | 3012 | .INPUTS 3013 | None. 3014 | 3015 | .OUTPUTS 3016 | Array of AD user objects. 3017 | 3018 | .NOTES 3019 | 3020 | .EXAMPLE 3021 | Get-OUUser -OrganizationalUnit "Users" 3022 | 3023 | Returns AD objects for all users in the Users organizational unit. 3024 | 3025 | .LINK 3026 | By Ben Peterson 3027 | linkedin.com/in/benponline 3028 | github.com/benponline 3029 | twitter.com/benponline 3030 | paypal.me/teknically 3031 | #> 3032 | 3033 | [cmdletbinding()] 3034 | param( 3035 | [parameter(ValueFromPipeline=$True,Mandatory=$True)] 3036 | [string]$OrganizationalUnit 3037 | ) 3038 | 3039 | begin{ 3040 | $domainInfo = (Get-ADDomain).DistinguishedName 3041 | $users = @() 3042 | } 3043 | 3044 | process{ 3045 | $users += Get-ADUser -Filter * -SearchBase "ou=$OrganizationalUnit,$domainInfo" 3046 | } 3047 | 3048 | end{ 3049 | return $users 3050 | } 3051 | } 3052 | 3053 | function Get-SubDirectorySize{ 3054 | <# 3055 | .SYNOPSIS 3056 | Gets directory names and sizes. 3057 | 3058 | .DESCRIPTION 3059 | Gets a list of directories and their sizes located at the submitted path. 3060 | 3061 | .PARAMETER Path 3062 | Path to the directory to be searched. 3063 | 3064 | .INPUTS 3065 | None. 3066 | 3067 | .OUTPUTS 3068 | PS objects with directory name and size in GB. 3069 | 3070 | .NOTES 3071 | 3072 | .EXAMPLE 3073 | Get-SubDirectorySize -Path 'C:\Users' 3074 | 3075 | Gets the name and size of all folders contained in the Users directory. 3076 | 3077 | .LINK 3078 | By Ben Peterson 3079 | linkedin.com/in/benponline 3080 | github.com/benponline 3081 | twitter.com/benponline 3082 | paypal.me/teknically 3083 | #> 3084 | 3085 | [CmdletBinding()] 3086 | Param( 3087 | [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3088 | [alias('FullName')] 3089 | [string] $Path 3090 | ) 3091 | 3092 | begin{ 3093 | $directorySizes = @() 3094 | } 3095 | 3096 | process{ 3097 | $directories = Get-ChildItem -Path $Path -Directory -Force 3098 | 3099 | foreach($dir in $directories){ 3100 | $directorySizes += Get-DirectorySize -Path $dir.FullName 3101 | } 3102 | } 3103 | 3104 | end{ 3105 | return $directorySizes 3106 | } 3107 | } 3108 | 3109 | function Get-UserActiveLogon{ 3110 | <# 3111 | .SYNOPSIS 3112 | Gets all computers where a user is logged in. 3113 | 3114 | .DESCRIPTION 3115 | Searches domain computers and returns a list of computers where a specific user is logged in. 3116 | 3117 | .PARAMETER SamAccountName 3118 | Takes the SamAccountName of an AD user. 3119 | 3120 | .PARAMETER OrganizationalUnit 3121 | Limits the function's search to an organizational unit. 3122 | 3123 | .INPUTS 3124 | String with SamAccountName or AD user object. Can pipe input to the function. 3125 | 3126 | .OUTPUTS 3127 | List of objects with the user name and the names of the computers they are logged into. 3128 | 3129 | .NOTES 3130 | Compatible with Windows 7 and newer. 3131 | 3132 | .EXAMPLE 3133 | Get-UserActiveLogon -Name Thor 3134 | 3135 | Returns a list of computers where Thor is logged in. 3136 | 3137 | .EXAMPLE 3138 | "Thor","Loki","Oden" | Get-UserActiveLogon 3139 | 3140 | Returns a list of computer where each of these users are logged in. 3141 | 3142 | .LINK 3143 | By Ben Peterson 3144 | linkedin.com/in/benponline 3145 | github.com/benponline 3146 | twitter.com/benponline 3147 | paypal.me/teknically 3148 | #> 3149 | 3150 | [CmdletBinding()] 3151 | Param( 3152 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3153 | [string]$SamAccountName, 3154 | [string]$OrganizationalUnit = "" 3155 | ) 3156 | 3157 | begin{ 3158 | $computerList = @() 3159 | 3160 | if($OrganizationalUnit -eq ""){ 3161 | $computers = (Get-ADComputer -Filter *).Name 3162 | }else{ 3163 | $computers = (Get-OUComputer -OrganizationalUnit $OrganizationalUnit).Name 3164 | } 3165 | 3166 | foreach ($computer in $computers){ 3167 | $computerList += [PSCustomObject]@{ 3168 | Computer = $computer; 3169 | UserName = "" 3170 | } 3171 | } 3172 | } 3173 | 3174 | process{ 3175 | $computerList | ForEach-Object -Parallel { 3176 | $currentUser = (Get-ComputerCurrentUser -Name $_.Computer).UserName 3177 | 3178 | if($using:SamAccountName -eq $currentUser){ 3179 | $_.UserName = $currentUser 3180 | } 3181 | } 3182 | } 3183 | 3184 | end{ 3185 | $computerList = $computerList | Where-Object -Property UserName -NE "" 3186 | return $computerList 3187 | } 3188 | } 3189 | 3190 | function Get-UserLastLogonTime{ 3191 | <# 3192 | .SYNOPSIS 3193 | Gets the last time a user logged onto the domain. 3194 | 3195 | .DESCRIPTION 3196 | Returns the last time a user or group of users logged onto the domain. 3197 | 3198 | .PARAMETER SamAccountName 3199 | User name. 3200 | 3201 | .INPUTS 3202 | You can pipe user names and user AD objects to this function. 3203 | 3204 | .OUTPUTS 3205 | PS objects with user name and last logon date. 3206 | 3207 | .NOTES 3208 | None. 3209 | 3210 | .EXAMPLE 3211 | Get-UserLastLogonTime -Name "Fred" 3212 | 3213 | Returns the last time Fred logged into the domain. 3214 | 3215 | .EXAMPLE 3216 | Get-ADUser -Filter * | Get-UserLastLogonTime 3217 | 3218 | Gets the last time all users in AD logged onto the domain. 3219 | 3220 | .LINK 3221 | By Ben Peterson 3222 | linkedin.com/in/benponline 3223 | github.com/benponline 3224 | twitter.com/benponline 3225 | paypal.me/teknically 3226 | #> 3227 | 3228 | [cmdletbinding()] 3229 | param( 3230 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3231 | [string]$SamAccountName = $env:UserName 3232 | ) 3233 | 3234 | begin{ 3235 | $lastLogonList = @() 3236 | 3237 | #When looking for AD User LastLogon we need to check all domain controllers because this information is not synced between them. 3238 | $domainControllers = (Get-ADDomainController -Filter *).Name 3239 | } 3240 | 3241 | process{ 3242 | $dcCount = $domainControllers.Count 3243 | 3244 | if($dcCount -eq 1){ 3245 | $lastLogonList += Get-ADUser $SamAccountName | 3246 | Get-ADObject -Properties LastLogon | 3247 | Select-Object -Property @{n="SamAccountName";e={$SamAccountName}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 3248 | }else{ 3249 | $lastLogonTime = Get-ADuser $SamAccountName -Server $domainControllers[0] | 3250 | Get-ADObject -Properties LastLogon | 3251 | Select-Object -Property @{n="SamAccountName";e={$SamAccountName}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 3252 | 3253 | for($i = 1; $i -LT $dcCount; $i++){ 3254 | $nextlogonTime = Get-ADuser $SamAccountName -Server $domainControllers[$i] | 3255 | Get-ADObject -Properties LastLogon | 3256 | Select-Object -Property @{n="SamAccountName";e={$SamAccountName}},@{n="LastLogon";e={([datetime]::fromfiletime($_.LastLogon))}} 3257 | 3258 | 3259 | if($nextlogonTime.LastLogon -GT $lastLogonTime.LastLogon){ 3260 | $lastLogonTime = $nextlogonTime 3261 | } 3262 | } 3263 | 3264 | $lastLogonList += $lastLogonTime 3265 | } 3266 | } 3267 | 3268 | end{ 3269 | return $lastLogonList 3270 | } 3271 | } 3272 | 3273 | function Disconnect-Users{ 3274 | <# 3275 | .SYNOPSIS 3276 | Signs out all users on a computer. 3277 | 3278 | .DESCRIPTION 3279 | This function signs out all users on a remote computer. 3280 | 3281 | .PARAMETER Name 3282 | This is the hostname of the computer where the users will be logged out. 3283 | 3284 | .INPUTS 3285 | AD computer objects. 3286 | 3287 | .OUTPUTS 3288 | None. 3289 | 3290 | .NOTES 3291 | None. 3292 | 3293 | .EXAMPLE 3294 | Disconnect-Users -Name Computer1 3295 | 3296 | This commmand signs out all users on "Computer1". 3297 | 3298 | .Example 3299 | Get-OUComputer -OrganizationalUnit "Department A" | Disconnect-Users 3300 | 3301 | This command signs out all users on all computer located in the "Department A" OU. 3302 | 3303 | .LINK 3304 | By Ben Peterson 3305 | linkedin.com/in/benponline 3306 | github.com/benponline 3307 | twitter.com/benponline 3308 | paypal.me/teknically 3309 | #> 3310 | 3311 | #[cmdletbinding(SupportsShouldProcess)] 3312 | param( 3313 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3314 | [Alias('ComputerName')] 3315 | [string]$Name 3316 | ) 3317 | 3318 | begin{ 3319 | $computers = @() 3320 | } 3321 | 3322 | process{ 3323 | $computers += $Name 3324 | } 3325 | 3326 | end{ 3327 | foreach($computer in $computers){ 3328 | Invoke-CimMethod -ClassName Win32_Operatingsystem -ComputerName $computer -MethodName Win32Shutdown -Arguments @{ Flags = 4 } | Out-Null 3329 | } 3330 | } 3331 | } 3332 | 3333 | function Move-Computer{ 3334 | <# 3335 | .SYNOPSIS 3336 | Moves a computer to an organizational unit. 3337 | 3338 | .DESCRIPTION 3339 | Moves a computer or computers to an AD organizational unit. 3340 | 3341 | .PARAMETER Name 3342 | This is the computer the function will move. 3343 | 3344 | .PARAMETER OrganizationalUnit 3345 | Destination organizational unit. 3346 | 3347 | .INPUTS 3348 | Host names or AD computer objects. 3349 | 3350 | .OUTPUTS 3351 | An array of AD computer objects. One for each computer moved by the function. 3352 | 3353 | .NOTES 3354 | 3355 | .EXAMPLE 3356 | Move-Computer -Name "Computer1" -OrganizationalUnit "Work Computers" 3357 | 3358 | Moves "Computer1" to the "Work Computers" OU. 3359 | 3360 | .EXAMPLE 3361 | "Computer1","Computer2" | Move-Computer -OrganizationalUnit "Work Computers" 3362 | 3363 | Moves "Computer1" and "Computer2" to the "Work Computers" OU. 3364 | 3365 | .EXAMPLE 3366 | Get-ADComputer -Filter * | Move-Computer -OrganizationalUnit "Work Computers" 3367 | 3368 | Moves all computers in AD to the "Work Computers" OU. 3369 | 3370 | .LINK 3371 | By Ben Peterson 3372 | linkedin.com/in/benponline 3373 | github.com/benponline 3374 | twitter.com/benponline 3375 | paypal.me/teknically 3376 | #> 3377 | 3378 | [cmdletbinding(SupportsShouldProcess)] 3379 | param( 3380 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3381 | [Alias('ComputerName')] 3382 | [string]$Name, 3383 | 3384 | [Parameter(Mandatory=$true)] 3385 | [string]$DestinationOU 3386 | ) 3387 | 3388 | begin{ 3389 | $domainInfo = (Get-ADDomain).DistinguishedName 3390 | $computers = @() 3391 | } 3392 | 3393 | process{ 3394 | $computers += Get-ADComputer $Name 3395 | } 3396 | 3397 | end{ 3398 | foreach ($computer in $computers){ 3399 | 3400 | if ($PSCmdlet.ShouldProcess($computer)) { 3401 | $computer | Move-ADObject -TargetPath "ou=$DestinationOU,$domainInfo" 3402 | } 3403 | } 3404 | } 3405 | } 3406 | 3407 | function Move-User{ 3408 | <# 3409 | .SYNOPSIS 3410 | Moves a user to an organizational unit. 3411 | 3412 | .DESCRIPTION 3413 | Moves a user or users to an AD organizational unit. 3414 | 3415 | .PARAMETER SamAccountName 3416 | This is the user the function will move. 3417 | 3418 | .PARAMETER OrganizationalUnit 3419 | Destination organizational unit. 3420 | 3421 | .INPUTS 3422 | Sam account names or AD user objects. 3423 | 3424 | .OUTPUTS 3425 | An array of AD user objects. One for each user moved by the function. 3426 | 3427 | .NOTES 3428 | 3429 | .EXAMPLE 3430 | Move-User -SamAccountName "User1" -OrganizationalUnit "Work Users" 3431 | 3432 | Moves "User1" to the "Work Users" OU. 3433 | 3434 | .EXAMPLE 3435 | "User1","User2" | Move-User -OrganizationalUnit "Work Users" 3436 | 3437 | Moves "User1" and "User2" to the "Work Users" OU. 3438 | 3439 | .EXAMPLE 3440 | Get-ADUser -Filter * | Move-User -OrganizationalUnit "Work Users" 3441 | 3442 | Moves all users in AD to the "Work Users" OU. 3443 | 3444 | .LINK 3445 | By Ben Peterson 3446 | linkedin.com/in/benponline 3447 | github.com/benponline 3448 | twitter.com/benponline 3449 | paypal.me/teknically 3450 | #> 3451 | 3452 | [cmdletbinding(SupportsShouldProcess)] 3453 | param( 3454 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3455 | [Alias("SamAccountName")] 3456 | [string]$Name, 3457 | 3458 | [Parameter(Mandatory=$true)] 3459 | [string]$DestinationOU 3460 | ) 3461 | 3462 | begin{ 3463 | $domainInfo = (Get-ADDomain).DistinguishedName 3464 | $users = @() 3465 | } 3466 | 3467 | process{ 3468 | $users += Get-ADUser $Name 3469 | } 3470 | 3471 | end{ 3472 | foreach ($user in $users){ 3473 | 3474 | if ($PSCmdlet.ShouldProcess($user)) { 3475 | $user | Move-ADObject -TargetPath "ou=$DestinationOU,$domainInfo" 3476 | } 3477 | } 3478 | } 3479 | } 3480 | 3481 | function Remove-Computer{ 3482 | <# 3483 | .SYNOPSIS 3484 | Removes a computer from Active Directory. 3485 | 3486 | .DESCRIPTION 3487 | Remove a computer or computers from AD. Also returns a list of computers that have been removed. 3488 | 3489 | .PARAMETER Name 3490 | Name of the computer being removed. 3491 | 3492 | .INPUTS 3493 | Host names or AD computer objects. 3494 | 3495 | .OUTPUTS 3496 | Returns an AD computer object for each computer removed from AD. 3497 | 3498 | .NOTES 3499 | 3500 | .EXAMPLE 3501 | Remove-Computer -Name "Computer1" 3502 | 3503 | Removes "Computer1" from AD. 3504 | 3505 | .EXAMPLE 3506 | "Computer1","Computer2" | Remove-Computer 3507 | 3508 | Removes "Computer1" and "Computer2" from AD. 3509 | 3510 | .EXAMPLE 3511 | Get-ADComputer -Filter * | Remove-Computer 3512 | 3513 | Removes all computers from AD. 3514 | 3515 | .LINK 3516 | By Ben Peterson 3517 | linkedin.com/in/benponline 3518 | github.com/benponline 3519 | twitter.com/benponline 3520 | paypal.me/teknically 3521 | #> 3522 | 3523 | [CmdletBinding(SupportsShouldProcess)] 3524 | Param( 3525 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3526 | [Alias('ComputerName')] 3527 | [string]$Name 3528 | ) 3529 | 3530 | begin{ 3531 | $computers = @() 3532 | } 3533 | 3534 | process{ 3535 | $computers += Get-ADComputer $Name 3536 | } 3537 | 3538 | end{ 3539 | foreach ($computer in $computers){ 3540 | 3541 | if ($PSCmdlet.ShouldProcess($computer)) { 3542 | $computer | Remove-ADComputer -Confirm:$false 3543 | } 3544 | } 3545 | } 3546 | } 3547 | 3548 | function Remove-DHCPReservation{ 3549 | <# 3550 | .SYNOPSIS 3551 | Removes a reservation for a computer in DHCP. 3552 | 3553 | .DESCRIPTION 3554 | This function removes an IPv4 reservation for a computer in all DHCP servers in a domain. 3555 | 3556 | .PARAMETER ComputerName 3557 | Host name of the computer in the reservation that will be removed. 3558 | 3559 | .INPUTS 3560 | None. 3561 | 3562 | .OUTPUTS 3563 | None. 3564 | 3565 | .NOTES 3566 | This function is meant to be used in a domain with one DHCP scope. 3567 | 3568 | This function will attempt to remove the reservation in each available DHCP server. If it is unable to reach every DHCP server, then the reservation will remain on that server if it exists there. 3569 | 3570 | .EXAMPLE 3571 | Remove-DHCPReservation -ComputerName "Computer1" 3572 | 3573 | Removes any reservations for "Computer1" in all available DHCP servers in the domain. 3574 | 3575 | .LINK 3576 | By Ben Peterson 3577 | linkedin.com/in/benponline 3578 | github.com/benponline 3579 | twitter.com/benponline 3580 | paypal.me/teknically 3581 | #> 3582 | 3583 | [cmdletbinding(SupportsShouldProcess)] 3584 | param( 3585 | [Parameter(Mandatory=$true)] 3586 | [string]$ComputerName 3587 | ) 3588 | 3589 | $dhcpServers = (Get-DhcpServerInDC).DnsName 3590 | $hostName = (Get-ADComputer -Identity $ComputerName).DNSHostName 3591 | 3592 | foreach($server in $dhcpServers){ 3593 | $dhcpServer = $server.split(".")[0] 3594 | $scopeId = (Get-DhcpServerv4Scope -ComputerName $dhcpServer | Select-Object -First 1).ScopeId 3595 | $dhcpLeases = Get-DhcpServerv4Lease -ComputerName $dhcpServer -ScopeId $scopeId -AllLeases 3596 | $clientId = ($dhcpLeases | Where-Object -Property HostName -EQ $hostName | Select-Object -First 1).ClientId 3597 | $reservations = Get-DhcpServerv4Reservation -ScopeId $scopeId -ComputerName $dhcpServer 3598 | 3599 | # Check if there is a reservation for the computer. 3600 | $isReserved = $false 3601 | foreach($r in $reservations){ 3602 | if($r.Name -EQ $hostName){ 3603 | $isReserved = $true 3604 | break 3605 | } 3606 | } 3607 | 3608 | if($isReserved -EQ $true){ 3609 | 3610 | if ($PSCmdlet.ShouldProcess($dhcpServer, "Removing DHCP reservation for $ComputerName")) { 3611 | Remove-DhcpServerv4Reservation -ScopeId $scopeId -ClientId $clientId -ComputerName $dhcpServer 3612 | } 3613 | } 3614 | } 3615 | } 3616 | 3617 | function Remove-User{ 3618 | <# 3619 | .SYNOPSIS 3620 | Removes a user from Active Directory. 3621 | 3622 | .DESCRIPTION 3623 | Removes a user or users from AD and returns a list of users that were removed. This function will not ask to confirm the deletion of accounts. 3624 | 3625 | .PARAMETER SamAccountName 3626 | User that will be removed. 3627 | 3628 | .INPUTS 3629 | None. You cannot pipe paramters to this function. 3630 | 3631 | .OUTPUTS 3632 | Returns an AD user object for each user removed from AD. 3633 | 3634 | .NOTES 3635 | 3636 | .EXAMPLE 3637 | Remove-User -Name "User1" 3638 | 3639 | Removes "User1" from AD. 3640 | 3641 | .EXAMPLE 3642 | "User1","User2" | Remove-User 3643 | 3644 | Removes "User1" and "User2" from AD. 3645 | 3646 | .EXAMPLE 3647 | Get-ADUser -Filter * | Remove-User 3648 | 3649 | Removes all Users from AD. 3650 | 3651 | .LINK 3652 | By Ben Peterson 3653 | linkedin.com/in/benponline 3654 | github.com/benponline 3655 | twitter.com/benponline 3656 | paypal.me/teknically 3657 | #> 3658 | 3659 | [CmdletBinding(SupportsShouldProcess)] 3660 | Param( 3661 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3662 | [string]$SamAccountName 3663 | ) 3664 | 3665 | begin{ 3666 | $users = @() 3667 | } 3668 | 3669 | process{ 3670 | $users += Get-ADUser $SamAccountName 3671 | } 3672 | 3673 | end{ 3674 | 3675 | foreach ($user in $users){ 3676 | 3677 | if ($PSCmdlet.ShouldProcess($user)) { 3678 | $user | Remove-ADUser -Confirm:$false 3679 | } 3680 | } 3681 | } 3682 | } 3683 | 3684 | function Set-ComputerIPAddress{ 3685 | <# 3686 | .SYNOPSIS 3687 | Sets the IP address of a computer. 3688 | 3689 | .DESCRIPTION 3690 | This function will set the IP address, subnet, DNS, and default gateway of a domain computer. The IP addresss is user provided and all the other information is 3691 | gathered from the computer executing the command. 3692 | 3693 | .PARAMETER ComputerName 3694 | The name of the remote computer that will be assigned the submitted IP address. 3695 | 3696 | .PARAMETER IPAddress 3697 | IP address that will be applied to the remote computer. 3698 | 3699 | .INPUTS 3700 | None. 3701 | 3702 | .OUTPUTS 3703 | None. 3704 | 3705 | .NOTES 3706 | This function attempts to change the IP settings on the remote computer using PowerShell commands. If that fails it will use netsh. 3707 | 3708 | This function only works on a computer that is set to use DHCP. Does not work on computers with a static IP address. 3709 | 3710 | Due to the nature of how this function connects to the remote computer, after changing the IP settings the function will say that the connection has been broken. This is a sign that the IP changes have worked. 3711 | 3712 | .EXAMPLE 3713 | Set-ComputerIPAddress -ComputerName "Computer2" -IPAddress 10.10.10.10 3714 | 3715 | Sets Computer2's IP address to 10.10.10.10 and copies over DNS, subnet, and default gateway information from the host computer. 3716 | 3717 | .LINK 3718 | By Ben Peterson 3719 | linkedin.com/in/benponline 3720 | github.com/benponline 3721 | twitter.com/benponline 3722 | paypal.me/teknically 3723 | #> 3724 | 3725 | [cmdletbinding()] 3726 | param( 3727 | [Parameter(Mandatory=$true)] 3728 | [string]$ComputerName, 3729 | 3730 | [Parameter(Mandatory=$true)] 3731 | [string]$IPAddress 3732 | ) 3733 | 3734 | Write-Host "Setting $ComputerName's IP address to $IPAddress. Connection to $ComputerName will be lost when the new IP address is active." 3735 | 3736 | #Self adapter 3737 | $SelfIPAddress = (Resolve-DnsName -Type A -Name $env:COMPUTERNAME).IPAddress 3738 | $SelfIPInterfaceIndex = (Get-NetIPAddress | Where-Object -Property IPAddress -eq $SelfIPAddress).InterfaceIndex 3739 | 3740 | #Subnetmask / Prefixlength 3741 | $SelfPrefixlength = (Get-NetIPAddress | Where-Object -Property IPAddress -eq $SelfIPAddress).PrefixLength 3742 | 3743 | #Default Gateway 3744 | $SelfDefaultGateway = (Get-NetRoute | Where-Object -Property DestinationPrefix -eq '0.0.0.0/0').NextHop 3745 | 3746 | #DNS 3747 | $SelfDNS = (Get-DnsClientServerAddress -InterfaceIndex $SelfIPInterfaceIndex -AddressFamily IPv4).ServerAddresses 3748 | $TargetIPAddress = (Resolve-DnsName -Type A -Name $ComputerName).IPAddress 3749 | 3750 | try{ 3751 | #Target interface index 3752 | $TargetIPInterfaceIndex = (Get-NetIPAddress -CimSession $ComputerName | Where-Object -Property IPAddress -eq $TargetIPAddress).InterfaceIndex 3753 | Set-DnsClientServerAddress -CimSession $ComputerName -InterfaceIndex $TargetIPInterfaceIndex -ServerAddresses $SelfDNS 3754 | New-NetIPAddress -CimSession $ComputerName -InterfaceIndex $TargetIPInterfaceIndex -IPAddress $IPAddress -AddressFamily IPv4 -PrefixLength $SelfPrefixlength -DefaultGateway $SelfDefaultGateway 3755 | }catch{ 3756 | switch($SelfPrefixlength){ 3757 | 30 {$SubnetMask = "255.255.255.252"} 3758 | 29 {$SubnetMask = "255.255.255.248"} 3759 | 28 {$SubnetMask = "255.255.255.240"} 3760 | 27 {$SubnetMask = "255.255.255.224"} 3761 | 26 {$SubnetMask = "255.255.255.192"} 3762 | 25 {$SubnetMask = "255.255.255.128"} 3763 | 24 {$SubnetMask = "255.255.255.0"} 3764 | 23 {$SubnetMask = "255.255.254.0"} 3765 | 22 {$SubnetMask = "255.255.252.0"} 3766 | 21 {$SubnetMask = "255.255.248.0"} 3767 | 20 {$SubnetMask = "255.255.240.0"} 3768 | 19 {$SubnetMask = "255.255.224.0"} 3769 | 18 {$SubnetMask = "255.255.192.0"} 3770 | 17 {$SubnetMask = "255.255.128.0"} 3771 | 16 {$SubnetMask = "255.255.0.0"} 3772 | } 3773 | 3774 | $TargetIPInterfaceAlias = "Local Area Connection" 3775 | 3776 | if($SelfDNS.count -gt 1){ 3777 | $SelfDNS1 = $SelfDNS[0] 3778 | $SelfDNS2 = $SelfDNS[1] 3779 | } 3780 | 3781 | Invoke-Command -ComputerName $ComputerName -ScriptBlock {netsh interface ip set dnsservers name="$using:TargetIPInterfaceAlias" address="$using:SelfDNS1" static primary} 3782 | 3783 | if($SelfDNS.count -gt 1){ 3784 | Invoke-Command -ComputerName $ComputerName -ScriptBlock {netsh interface ip add dnsservers name="$using:TargetIPInterfaceAlias" address="$using:SelfDNS2"} 3785 | } 3786 | 3787 | Invoke-Command -ComputerName $ComputerName -ScriptBlock {netsh interface ip set address $using:TargetIPInterfaceAlias static $using:IPAddress $using:SubnetMask $using:SelfDefaultGateway} 3788 | } 3789 | } 3790 | 3791 | function Set-UserChangePassword{ 3792 | <# 3793 | .SYNOPSIS 3794 | Sets user account to require a password change at the next log on. 3795 | 3796 | .DESCRIPTION 3797 | This function requires the AD user accounts passed to it to require the user to create a new password at the their next login. 3798 | If the account's 'PasswordNeverExpires' tag is set to true, then it is not affected by this function. 3799 | 3800 | .PARAMETER Name 3801 | This is the SamAccountName of the user you want to require a new password for. 3802 | 3803 | .INPUTS 3804 | Can take AD user objects as input. 3805 | 3806 | .OUTPUTS 3807 | AD user objects that have been tagged for creating a new password on log in. 3808 | 3809 | .NOTES 3810 | 3811 | .EXAMPLE 3812 | Reset-UserPassword -Name 'billy' 3813 | 3814 | Requires the AD account with the SamAccountID of 'billy' to create a new password on next login. 3815 | 3816 | .EXAMPLE 3817 | Get-ADUser -Filter * | Reset-UserPassword 3818 | 3819 | Requires all users in AD to create a new password on next login. 3820 | 3821 | .Example 3822 | Get-OUUser -OrganizationalUnit 'Users' | Reset-UserPassword 3823 | 3824 | Requires all users in the 'Users' OU to create a new password when they log in next. Get-OUUser 3825 | is a function from the PSSystemAdministrator module. 3826 | 3827 | .LINK 3828 | By Ben Peterson 3829 | linkedin.com/in/benponline 3830 | github.com/benponline 3831 | twitter.com/benponline 3832 | paypal.me/teknically 3833 | #> 3834 | 3835 | [cmdletbinding()] 3836 | param( 3837 | [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)] 3838 | [Alias("Name")] 3839 | [string]$SamAccountName 3840 | ) 3841 | 3842 | begin{ 3843 | $users = @() 3844 | } 3845 | 3846 | process{ 3847 | $users += Get-ADUser $SamAccountName 3848 | } 3849 | 3850 | end{ 3851 | $users | Set-ADUser -ChangePasswordAtLogon $true 3852 | } 3853 | } 3854 | 3855 | function Start-Computer{ 3856 | <# 3857 | .SYNOPSIS 3858 | Starts a remote computer by sending a magic packet. 3859 | 3860 | .DESCRIPTION 3861 | Can start a single or multiple computers on a Windows domian. 3862 | 3863 | .PARAMETER Name 3864 | Name of the computer this function will start. 3865 | 3866 | .INPUTS 3867 | You can pipe AD computer PS objects to this function. 3868 | 3869 | .OUTPUTS 3870 | None. 3871 | 3872 | .NOTES 3873 | Run Enable-WakeOnLan, a function in the PSSystemAdministrator module, on computers that you want to start using this function. This will ensure Start-Computer will work on them. 3874 | 3875 | May not work on computers with manually set static IP addresses. DHCP reservations should work. 3876 | 3877 | .EXAMPLE 3878 | Start-Computer -Name SERVER1 3879 | 3880 | Starts SERVER1 if it is powered down. 3881 | 3882 | .EXAMPLE 3883 | "Computer1","Computer2" | Start-Computer 3884 | 3885 | .EXAMPLE 3886 | Get-ADComputer -Filter * | Start-Computer 3887 | 3888 | Attempts to start all computers that are part of the domain. 3889 | 3890 | .LINK 3891 | By Ben Peterson 3892 | linkedin.com/in/benponline 3893 | github.com/benponline 3894 | twitter.com/benponline 3895 | paypal.me/teknically 3896 | 3897 | .LINK 3898 | Based on: https://gallery.technet.microsoft.com/scriptcenter/Send-WOL-packet-using-0638be7b/view/Discussions#content 3899 | #> 3900 | 3901 | [CmdletBinding()] 3902 | param( 3903 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3904 | [Alias("ComputerName")] 3905 | [string]$Name 3906 | ) 3907 | 3908 | begin{ 3909 | $domainController = (Get-ADDomainController).Name 3910 | $scopeID = (Get-DhcpServerv4Scope -ComputerName $domainController).ScopeID 3911 | $UdpClient = New-Object System.Net.Sockets.UdpClient 3912 | $UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7) 3913 | } 3914 | 3915 | process{ 3916 | $computerMAC = (Get-DhcpServerv4Lease -ComputerName $domainController -ScopeId $scopeID | Where-Object -Property hostname -match $Name).clientid 3917 | $MacByteArray = $computerMAC -split "-" | ForEach-Object { "0x$_"} 3918 | $MagicPacketHeader = "0xFF","0xFF","0xFF","0xFF","0xFF","0xFF" 3919 | $MagicPacketRaw = $MagicPacketHeader + ($MacByteArray * 16) 3920 | $MagicPacket = $MagicPacketRaw | ForEach-Object { [Byte]$_ } 3921 | $UdpClient.Send($MagicPacket,$MagicPacket.Length) | Out-Null 3922 | Write-Host "Magic packet sent to $Name." 3923 | } 3924 | 3925 | end{ 3926 | $UdpClient.Close() 3927 | } 3928 | } 3929 | 3930 | function Test-NetworkSpeed{ 3931 | <# 3932 | .SYNOPSIS 3933 | Tests the network speed between the machine running this function and a remote machine. 3934 | 3935 | .DESCRIPTION 3936 | This function measures the network speed, in MB per second, between the machine running this function and a remote machine. It does this by creating a text file in the directory the module is located and copying it to a remote directory supplied by the user. 3937 | 3938 | The user must provide a directory to test the speed to. The user can adjust the size of the file in MB. The function will copy the file over to the destination directory 5 times and calculate the average MB per second. The user can adjust the number of tests. 3939 | 3940 | .PARAMETER DestinationDirectory 3941 | The function will test the speed between the local machine and this location. 3942 | 3943 | .PARAMETER Count 3944 | The number of times the speed will be tested before calculating the average MB per second. 3945 | 3946 | .PARAMETER FileSizeMB 3947 | The size of the file that will be used to test the network speed. 3948 | 3949 | .INPUTS 3950 | PS objects with a property named "FullName" or "DestinationDirectory". 3951 | 3952 | .OUTPUTS 3953 | One PS Object per DestinationDirectory given: 3954 | [string]SourceMachine Name of machine running the function. 3955 | [string]DestinationDirectory Speed test between here and SourceMachine. 3956 | [int]FileSizeMB Size of file. 3957 | MbPerSecond Speed in MB per second. 3958 | 3959 | .NOTES 3960 | This function does not return consistent results. Do not assume the results returned are completely accurate. 3961 | 3962 | .EXAMPLE 3963 | Test-NetworkSpeed -DestinationDirectory "\\Server\Share" 3964 | 3965 | Measures the network speed between the host machine and the computer hosting the file share by copying a 100 MB text file to the destination 5 times and calculating the average speed in MB per second. 3966 | 3967 | .EXAMPLE 3968 | Test-NetworkSpeed -DestinationDirectory "\\Server\Share" -Count 10 -FileSizeMB 200 3969 | 3970 | Measures the network speed between the host machine and the computer hosting the file share by copying a 200 MB text file to the destination 10 times and calculating the average speed in MB per second. 3971 | 3972 | .EXAMPLE 3973 | Test-NetworkSpeed -DestinationDirectory "\\Server\Share","\\Server2\Share" -Count 10 -FileSizeMB 200 3974 | 3975 | Measures the network speed between the host machine and the computers hosting the file shares by copying a 200 MB text file to the destinations 10 times each and calculating the average speed in MB per second. 3976 | 3977 | .EXAMPLE 3978 | "\\Server\Share","\\Server2\Share" | Test-NetworkSpeed -Count 10 -FileSizeMB 200 3979 | 3980 | Measures the network speed between the host machine and the computers hosting the file shares by copying a 200 MB text file to the destinations 10 times each and calculating the average speed in MB per second. 3981 | 3982 | .LINK 3983 | By Ben Peterson 3984 | linkedin.com/in/benponline 3985 | github.com/benponline 3986 | twitter.com/benponline 3987 | paypal.me/teknically 3988 | #> 3989 | 3990 | [CmdletBinding()] 3991 | param( 3992 | [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)] 3993 | [Alias("FullName")] 3994 | [string[]]$DestinationDirectory, 3995 | 3996 | [int]$Count = 5, 3997 | 3998 | [ValidateRange(1,1999)] 3999 | [int]$FileSizeMB = 100 4000 | ) 4001 | 4002 | begin{ 4003 | $sourceFilePath = "$PSScriptRoot\TextFile.txt" 4004 | $fileContent = "1" * (1MB * $FileSizeMB) 4005 | New-Item -Path $sourceFilePath -ItemType File -Value $fileContent -Force | Out-Null 4006 | $results = @() 4007 | } 4008 | 4009 | process{ 4010 | foreach($dir in $DestinationDirectory){ 4011 | $destinationFilePath = "$dir\TextFile.txt" 4012 | $totalMBPerSecond = 0 4013 | 4014 | for ($i = 0; $i -lt $Count; $i++) { 4015 | $copy = Measure-Command -Expression { Copy-Item -Path $sourceFilePath -Destination $destinationFilePath -Force } 4016 | $copyMilliseconds = $copy.Milliseconds 4017 | $MBPerSecond = [Math]::Round($FileSizeMB / $copyMilliseconds * 1000) 4018 | $totalMBPerSecond += $MBPerSecond 4019 | } 4020 | 4021 | $averageMBPerSecond = [Math]::Round($totalMBPerSecond / $count) 4022 | 4023 | $results += [PSCustomObject]@{ 4024 | SourceMachine = $env:ComputerName; 4025 | DestinationDirectory = $dir; 4026 | FileSizeMB = $FileSizeMB; 4027 | MbPerSecond = $averageMBPerSecond 4028 | } 4029 | 4030 | Remove-Item -Path $destinationFilePath -Force 4031 | } 4032 | } 4033 | 4034 | end{ 4035 | Remove-Item -Path $sourceFilePath -Force 4036 | return $results 4037 | } 4038 | } 4039 | 4040 | function Unlock-User{ 4041 | <# 4042 | .SYNOPSIS 4043 | Unlocks a user account. 4044 | 4045 | .DESCRIPTION 4046 | Unlocks an Active Directory user or group of users by passing SamAccountNames or user AD objects to this funtion. You can unlock a user account that was locked out due to too many failed logon attempts. 4047 | 4048 | .PARAMETER SamAccountName 4049 | This is the user name of the user that will be unlocked. 4050 | 4051 | .INPUTS 4052 | User AD objects can be passed to this function. 4053 | 4054 | .OUTPUTS 4055 | None. 4056 | 4057 | .NOTES 4058 | None. 4059 | 4060 | .EXAMPLE 4061 | Unlock-User -Name "User1" 4062 | 4063 | Unlocks the user named User1 in Active Directory. 4064 | 4065 | .EXAMPLE 4066 | "User1","User2" | Unlock-User 4067 | 4068 | Unlocks users User1 and User2 in Active Directory. 4069 | 4070 | .EXAMPLE 4071 | Get-ADUser "User1" | Unlock-User 4072 | 4073 | Unlocks User1 in Active Directory. 4074 | 4075 | .LINK 4076 | By Ben Peterson 4077 | linkedin.com/in/benponline 4078 | github.com/benponline 4079 | twitter.com/benponline 4080 | paypal.me/teknically 4081 | #> 4082 | 4083 | [cmdletbinding()] 4084 | param( 4085 | [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Mandatory=$True)] 4086 | [string]$SamAccountName 4087 | ) 4088 | 4089 | begin{ 4090 | $users = [System.Collections.Generic.List[psobject]]::new() 4091 | } 4092 | 4093 | process{ 4094 | $users.Add((Get-ADUser $SamAccountName)) 4095 | } 4096 | 4097 | end{ 4098 | $users | Unlock-ADAccount 4099 | } 4100 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSSystemAdministrator 2 | This module contains functions useful for administrating a Windows Active Directory domain with a single DHCP scope. Many of the functions are designed to work with eachother. Functions that gather information on users or computers can be piped into functions that take an action. For instance, you can pipe a function that returns computers that have not logged onto the network for 30 days into a function that disables computer accounts. In a single line of code, you can disable all the inactive computers in active directory. There are a wide variety of functions that perform other tasks like sending magic packets for wake on lan, measuring directory and sub-directory sizes, gathering large files, and other tasks. Every function is fully documented and works with the `Get-Help` function. 3 | 4 | This module is written for PowerShell Core and tested with Windows 10 machines. I am actively developing this module alongside my work as a system administrator. I use this module every day. 5 | 6 | ## Installation 7 | 1. Download the PSSystemAdministrator.psm1 file. 8 | 2. Open the PowerShell modules folder on your computer. 9 | - For PowerShell Core it is located here: \Documents\PowerShell\Modules 10 | 3. Create a folder named “PSSystemAdministrator”. 11 | 4. Place the PSSystemAdministrator.psm1 file in the new folder. 12 | 5. Open your version of PowerShell and run the following command: 13 | `Set-ExecutionPolicy -ExecutionPolicy RemoteSigned` 14 | 6. This will allow PowerShell to read the contents of the module. 15 | 7. Open a new PowerShell session and you are good to go. 16 | 17 | ## Functions 18 | `Add-DHCPReservation` Adds a reservation in DHCP for a computer. 19 | 20 | `Disable-Computer` Disables a computer. 21 | 22 | `Disable-User` Disables a user. 23 | 24 | `Disconnect-Users` Signs out all users on a computer. 25 | 26 | `Enable-Computer` Enables an AD computer account. 27 | 28 | `Enable-User` Enables an AD user account. 29 | 30 | `Enable-WakeOnLan` Configures a computer to allow wake on lan. 31 | 32 | `Get-AccessedFile` Gets all files in a directory that have been accessed in the last 24 hours. 33 | 34 | `Get-ActiveComputer` Gets a list of computers that have logged onto the domain in the last 30 days. 35 | 36 | `Get-ActiveFile` Gets all files in a directory that have been written in the last 24 hours. 37 | 38 | `Get-ActiveUser` Gets a list of all users that have logged on in the last 30 days. 39 | 40 | `Get-ChildItemLastAccessTime` Gets all files in a directory and returns information including file name and last access time. 41 | 42 | `Get-ChildItemLastWriteTime` Gets all files in a directory and returns information including last write time. 43 | 44 | `Get-ComputerCurrentUser` Gets the current user logged onto a computer. 45 | 46 | `Get-ComputerDriveInformation` Gets information about the drives on a computer. 47 | 48 | `Get-ComputerFailedLogonEvent` Gets failed logon events from a computer in the last day. 49 | 50 | `Get-ComputerInformation` Gets general information about a computer. 51 | 52 | `Get-ComputerIPAddress` Gets the IPv4 address of a computer. 53 | 54 | `Get-ComputerLastBootUpTime` Gets the last time a computer booted up. 55 | 56 | `Get-ComputerLastLogonTime` Gets the last time a computer logged onto the domain. 57 | 58 | `Get-ComputerMemory` Gets the memory in GB of a computer. 59 | 60 | `Get-ComputerModel` Gets the model of a computer. 61 | 62 | `Get-ComputerOS` Gets the operating system name of a computer. 63 | 64 | `Get-ComputerPhysicalDiskInformation` Gets information about the physical disks of a computer. 65 | 66 | `Get-ComputerProcessor` Gets the processor of a computer. 67 | 68 | `Get-ComputerShareFolder` Gets all of the share folders on a computer. 69 | 70 | `Get-ComputerSoftware` Gets all of the installed software on a computer. 71 | 72 | `Get-ComputerSystemEvent` Gets system events from a computer. 73 | 74 | `Get-CredentialExportToXML` Gets credentials from the user and exports them to location provided by the user. 75 | 76 | `Get-DHCPReservation` Gets all reservations for a computer in DHCP. 77 | 78 | `Get-DirectorySize` Gets the size of a directory. 79 | 80 | `Get-DisabledComputer` Gets a list of all computers that are disabled. 81 | 82 | `Get-DisabledUser` Gets a list of all users that are disabled. 83 | 84 | `Get-InactiveComputer` Gets computers that have not logged onto the domain for more than 30 days. 85 | 86 | `Get-InactiveFile` Gets all files in a directory that have not been accessed in the last 24 hours. 87 | 88 | `Get-InactiveUser` Gets a list of all users that have not logged on to the domain for 30 days. 89 | 90 | `Get-ItemLastAccessTime` Gets the last access time from an item. 91 | 92 | `Get-ItemLastWriteTime` Gets the last write time from an item. 93 | 94 | `Get-LargeFile` Gets files larger than 500 MB from a directory. 95 | 96 | `Get-LockedOutUser` Gets locked out users from Active Directory. 97 | 98 | `Get-LockedOutUserEvent` Gets events about user accounts getting locked in Active Directory. 99 | 100 | `Get-OfflineComputer` Gets all computers that are offline. 101 | 102 | `Get-OnlineComputer` Gets computers that are online. 103 | 104 | `Get-OUComputer` Gets computers from a specific organizational unit. 105 | 106 | `Get-OUUser` Gets users from a specific organizational unit. 107 | 108 | `Get-SubDirectorySize` Gets directory names and sizes. 109 | 110 | `Get-UserActiveLogon` Gets all computers where a user is logged in. 111 | 112 | `Get-UserLastLogonTime` Gets the last time a user logged onto the domain. 113 | 114 | `Move-Computer` Moves a computer to an organizational unit. 115 | 116 | `Move-User` Moves a user to an organizational unit. 117 | 118 | `Remove-Computer` Removes a computer from Active Directory. 119 | 120 | `Remove-DHCPReservation` Removes a reservation for a computer in DHCP. 121 | 122 | `Remove-User` Removes a user from Active Directory. 123 | 124 | `Set-ComputerIPAddress` Sets the IP address of a computer. 125 | 126 | `Set-UserChangePassword` Sets user account to require a password change at the next log on. 127 | 128 | `Start-Computer` Starts a remote computer by sending a magic packet. 129 | 130 | `Test-NetworkSpeed` Tests the network speed between the machine running this function and a remote machine. 131 | 132 | `Unlock-User` Unlocks an AD user account. 133 | --------------------------------------------------------------------------------