diff --git a/README.md b/README.md index 52bce95..69ea302 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ - \ No newline at end of file +# AD Commands and Scripts + +This is a collection of Command Prompt and PowerShell commands and scripts I have collected and built for working in Windows Active Directory environment. Mostly for my own ease of access from anywhere. But I figured might as well release them into the wild. + +There's nothing crazy in here (yet). Just some things I didn't want to commit to memory. These assume you have some know how, but not much. I've made READMEs, comments, and other notes where I felt appropriate. But please read and understand each command/script before using. And of course, feel free to tweak to your own needs. + +### Table of Contents: +cmd-commands.md = A document collecting Windows Command Line commands +ps-commands.md = A document collecting Windows PowerShell commands +ps-scripts/ = A folder containing various PowerShell scripts \ No newline at end of file diff --git a/adaccount-expiry/README.md b/adaccount-expiry/README.md deleted file mode 100755 index e2336e5..0000000 --- a/adaccount-expiry/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# ADAccount Expiry PS Script v22.01.1 - -ADAccount Expiry is a PowerShell script that allows AD Admins to export into the Script's Current Directory a CSV report on Active Directory Accounts that are Expired, Expiring within 2 weeks, and/or Disabled.. - -## Prerequisites - -Before you begin, ensure you have met the following requirements: -* PowerShell with the [ActiveDirectory](https://docs.microsoft.com/en-us/powershell/module/activedirectory/?view=windowsserver2022-ps) module installed (Windows only). -* Proper permissions granted by your Admin. - -## Installing ADAccount Expiry - -To install ADAccount Expiry, follow these steps: - -* Download the release Source code and place the contents into folder at a location of your choosing. - -## Using ADAccount Expiry - -To use ADAccount Expiry, follow these steps: - -``` -- Run the script in PowerShell -- The report will be exported to the current directory -- Open the CSV in Excel -- File > Save As . change extension dropdown to "*.xlsx" > Save -- You may now apply any formatting/editing to the report prior to submitting it -``` - -This project uses the following license: [Unlicense](https://unlicense.org). diff --git a/adaccount-expiry/ROADMAP.md b/adaccount-expiry/ROADMAP.md deleted file mode 100755 index c9ab325..0000000 --- a/adaccount-expiry/ROADMAP.md +++ /dev/null @@ -1,4 +0,0 @@ -# Roadmap - -* Add ability to email results upon completion of script -* Add ability to automate running of script on schedule diff --git a/check-free-space/README.md b/check-free-space/README.md deleted file mode 100755 index 5c503e3..0000000 --- a/check-free-space/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Check Free Space PS Script v22.01.1 - -Check Free Space is a PowerShell script that allows AD Admins to check the used/max HDD space on all drives on a remote PC on the domain. - -## Prerequisites - -Before you begin, ensure you have met the following requirements: -* PowerShell with the [ActiveDirectory](https://docs.microsoft.com/en-us/powershell/module/activedirectory/?view=windowsserver2022-ps) module installed (Windows only). -* Proper permissions granted by your Admin. - -## Installing Check Free Space - -To install Check Free Space, follow these steps: - -* Download the release Source code and place the contents into folder at a location of your choosing. - -## Using Check Free Space - -To use Check Free Space, follow these steps: - -``` -- Open the script in a notepad program -- Enter the hostname of the target PC in the "HOSTNAME" field, replacing the word already there -- Save the file -- Run the script in PowerShell -- The results will be printed out in the terminal -``` - -This project uses the following license: [Unlicense](https://unlicense.org). diff --git a/check-free-space/ROADMAP.md b/check-free-space/ROADMAP.md deleted file mode 100755 index 6649c47..0000000 --- a/check-free-space/ROADMAP.md +++ /dev/null @@ -1,3 +0,0 @@ -# Roadmap - -* N/A diff --git a/cmd-commands.md b/cmd-commands.md new file mode 100644 index 0000000..fca518a --- /dev/null +++ b/cmd-commands.md @@ -0,0 +1,69 @@ +### Network Reset + +Run the following command in an elevated Command Prompt: + +``` +ipconfig /flushdns && ipconfig /release && ipconfig /renew && ipconfig /registerdns && netsh winsock reset && shutdown /r +``` + +This will, in order: +1. Flush the DNS Records +2. Release the IP Address +3. Renew the IP Address +4. Re-registers the DNS Records +5. Resets the Winsock Catalog (requires a reboot after) +6. Restarts the PC + +Be cautious if running these commands while remotely connecting to another computer. The "&&" operator tells CMD to run the commands one after the other. If you run them one at a time instead, you will lose connection to the computer before you can complete the rest. It might be best to have someone on the other end in case you lose connection, or the computer does not regain connection after the restart. + +
+ +### SFC & DISM Commands + +The following commands will repair/replace corrupted/missing system files, as well as clear up disk space. They must all be ran in an elevated Command Prompt: + +1. +``` +sfc /scannow +``` + +If it reports no issues found or all issues fixed, you are done. or, optionally skip to Step 6. Otherwise, continue to Step 2. + +2. +``` +DISM /Online /Cleanup-Image /CheckHealth +``` + +3. +``` +DISM /Online /Cleanup-Image /ScanHealth +``` + +4. +``` +DISM /Online /Cleanup-Image /RestoreHealth +``` + +5. Repeat Step 1 + +Note: the following optional steps will free up disk space, but also remove files allowing uninstallation of Windows Updates + +6. +``` +DISM /Online /Cleanup-Image /AnalyzeComponentStore +``` + +If prompted to reboot, do so + +7. +``` +DISM /Online /Cleanup-Image /StartComponentCleanup +``` + +8. +``` +DISM /Online /Cleanup-Image /StartComponentCleanup /ResetBase +``` + +
+ diff --git a/ps-commands.md b/ps-commands.md new file mode 100644 index 0000000..dea2247 --- /dev/null +++ b/ps-commands.md @@ -0,0 +1,43 @@ +# PS Commands + +Note: unless otherwise stated, replace any variable such as `$ComputerName` with the appropriate data. + +### Fetch (some) Installed Programs + +```powershell +Get-WmiObject win32_product -ComputerName $ComputerName | select Name, version +``` + +
+ +### Fetch AD Groups with $SearchTerm in them + +```powershell +Get-ADGroup -Filter 'name -like "*$SearchTerm*"' | Sort-Object | select Name +``` + +
+ +### Fetch Boot Time + +```powershell +Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $ComputerName | slect csname, lastbootuptime +``` + +
+ +### Fetch Computer Group Membership + +```powershell +Get-ADPrincipalGroupMembership (Get-ADComputer $ComputerName) | select-object name +``` + +
+ +### Fetch User Group Membership + +```powershell +Get-ADPrincipalGroupMembership (Get-ADUser $Username) | select-object name +``` + +
\ No newline at end of file diff --git a/ps-scripts/ad-account-expiry/README.md b/ps-scripts/ad-account-expiry/README.md new file mode 100755 index 0000000..f5071fb --- /dev/null +++ b/ps-scripts/ad-account-expiry/README.md @@ -0,0 +1,24 @@ +# AD Account Expiry PS Script + +ADAccount Expiry is a PowerShell script that allows AD Admins to export into the Script's Current Directory a CSV report on Active Directory Accounts that are Expired, Expiring within 2 weeks, and/or Disabled.. + +
+ +### Prerequisites + +Before you begin, ensure you have met the following requirements: +- PowerShell with the [ActiveDirectory](https://docs.microsoft.com/en-us/powershell/module/activedirectory/?view=windowsserver2022-ps) module installed (Windows only). +- Proper permissions granted by your Admin. + +
+ +### Using ADAccount Expiry + +To use AD Account Expiry, follow these steps: + +1. Download the `ad-account-expiry` folder and place at a location of your choosing +2. Run the script in PowerShell +3. The report will be exported to the current directory +4. Open the CSV in Excel +5. File > Save As . change extension dropdown to "*.xlsx" > Save +6. You may now apply any formatting/editing to the report prior to submitting it \ No newline at end of file diff --git a/adaccount-expiry/ADAccount-Expiry.ps1 b/ps-scripts/ad-account-expiry/ad-account-expiry.ps1 similarity index 91% rename from adaccount-expiry/ADAccount-Expiry.ps1 rename to ps-scripts/ad-account-expiry/ad-account-expiry.ps1 index 03433f2..9c9724b 100755 --- a/adaccount-expiry/ADAccount-Expiry.ps1 +++ b/ps-scripts/ad-account-expiry/ad-account-expiry.ps1 @@ -1,7 +1,4 @@ # AD Account Expiration Script -# -# Please see the README. -# -Tack $expired = Search-ADAccount -AccountExpired | Select Name, SamAccountName, Manager, Enabled, AccountExpirationDate $expiring = Search-ADAccount -AccountExpiring -TimeSpan "14" | Select Name, SamAccountName, Manager, Enabled, AccountExpirationDate diff --git a/ps-scripts/check-free-space/README.md b/ps-scripts/check-free-space/README.md new file mode 100755 index 0000000..e7e4736 --- /dev/null +++ b/ps-scripts/check-free-space/README.md @@ -0,0 +1,30 @@ +# Check Free Space PS Script + +Check Free Space is a PowerShell script that allows AD Admins to check the used/max HDD space on all drives on a remote PC on the domain. + +
+ +## Prerequisites + +Before you begin, ensure you have met the following requirements: +- PowerShell with the [ActiveDirectory](https://docs.microsoft.com/en-us/powershell/module/activedirectory/?view=windowsserver2022-ps) module installed (Windows only). +- Proper permissions granted by your Admin. + +
+ +### Using Check Free Space + +To use Check Free Space, follow these steps: + +1. Download the `check-free-space` folder and place at a location of your choosing +2. Open the script in a notepad program +3. Replace $hostname with the hostname of the target PC +4. Save the file +5. Run the script in PowerShell +6. The results will be printed out in the terminal + +
+ +### Notes + +Moving forward, future PowerShell versions no longer support Get-WmiObject. So if all of a sudden you see the error message like “RPC Server is unavailable”, it’s probably time to switch over to Get-CimInstance instead. The parameters are the same for Win32_LogicalDisk. Simply replace Get-WmiObject with Get-CimInstance and you are good to go. diff --git a/check-free-space/Check-Free-Space.ps1 b/ps-scripts/check-free-space/check-free-space.ps1 similarity index 52% rename from check-free-space/Check-Free-Space.ps1 rename to ps-scripts/check-free-space/check-free-space.ps1 index baa1ec3..2e388b2 100755 --- a/check-free-space/Check-Free-Space.ps1 +++ b/ps-scripts/check-free-space/check-free-space.ps1 @@ -1,4 +1,6 @@ -$servers = @("HOSTNAME") +# Check Free Space Script + +$servers = @($hostname) # Replace $hostname with the computer's hostname Foreach ($server in $servers) { @@ -15,6 +17,3 @@ Foreach ($server in $servers) } } - -# /Update on Dec 9, 2019/ -# Note that moving forward, future PowerShell versions no longer support Get-WmiObject so if all of a sudden you see the error message like “RPC Server is unavailable”, it’s probably time to switch over to Get-CimInstance instead. The parameters are the same for Win32_LogicalDisk. Simply replace Get-WmiObject with Get-CimInstance and you are good to go.