Powershell Scripts

Free Powershell scripts ethusiasts or for use in business enviroments

View them on Github
powershell

Command/Installation Menu

A Powershell script that shows a menu of user-defined commands to perform. Also has an "Automatic" option to run through all menu options, waiting for a Y/N input.


Getting Started

Download, unzip, and open the menu.ps1 in any text editor.

Download

Ascii Art

The Ascii art is optional. If you do not want it, remove line 31, containing the code write-host $logo. If you want to replace it, use a Ascii generator tool to make a customer banner. I use the one found here. Be sure the new banner is put in-between the "@ symbols.

Customizing the Menu

Start by modifying the main menu. The name of the menu option name start immediatly after the syntax write-host. There are only 7 options here by default, but more can be appending additional write-host commands to the bottom of the list.

write-host $logo
write-host
#Main Menu
write-host 1. Automatic 1
write-host 2. Automatic 2
write-host 3. Manual 1
write-host 4. Manual 2
write-host 5. Manual 3
write-host 6. Manual 4
write-host 7. Manual 5
#Add additional menu options here



Modifying Commands

The menu works by having a series of if-else statements. These statements wait for a specific number to be pressed, corresponding to one of the if statements. In the example below, the if statement waits for a input on the menu. If the input is 1, the code inside the block will run.

if ($menu_input -eq '1') {
write-host Starting action because '1' was pressed
#Powershell command(s) goes here
write-host Confirmation of action



Automatic Mode

This script uses a auto-prompt mode that runs through a series of Yes/No questions and executes code based on the answer. For example, this automatic mode could be used to install software on employee machines at a business. It could automatically install anti-virus, email, a web browser etc, and than prompt with a simple Yes/No for additional software that not every employee needs, such as accounting software or IT tools.

An example of what this would look like in code is below:

if ($menu_input -eq '1') {
write-host Automatic action as soon as menu option is selected
write-host
####Powershell command(s) that automatically run without a Yes/No prompt go here####
write-host Confirmation of automatic actions complete
write-host
###################### Start of the Yes/No Prompts #####################################
$prompt1response = read-host -prompt "Prompt 1 [Y/N]"
if ($prompt1response -eq 'y') {
write-host
write-host Executing prompt 1 because "y" was pressed on the keyboard
#Powershell command(s) goes here
write-host Confirmation of prompt 1
write-host
read-host Press ENTER to continue...
} elseif ($prompt1response -eq 'n') {
write-host
write-host Skipping because "n" was pressed on the keyboard
Write-Host}
$prompt2response = read-host -prompt "Prompt 2 [Y/N]"



Manual Mode

Manual mode is essentially just selecting a option from the main menu and having it run a specific task (or series of tasks)

Assuming this is option 2 or greater, the elseif statement will be required. The code will look similar to this:

} elseif ($menu_input -eq '2') {
write-host
write-host Manual option 2
#Powershell command(s) for option 2 go here
write-host
read-host Press ENTER to continue...
&$menu



Running the Script

In order to run Powershell scripts on windows, the execution policy needs to be changed. This can be done in an administrative Powershell window by running set-executionpolicy remotesigned.

Personally, I find it easier to run Powershell ISE as an administrator and then copy the command located at the start of the script and run it from there.

New AD Domain user

A Powershell script to make a new user on a existing domain. Usernames are 8 characters long, consisting of the first letter of the first name, followed by the first seven letters of the last name. Comes with some advanced options.


Getting Started

Download, unzip, and open the new_user.ps1 in any text editor.

Download

Making Neccessary Changes

In order for this script to work, you must change some variables in the script tailored toward your network.



Changing the Default Password

This script is set up to give all new users the same universal password, stored in cleartext on this script. The variable -ChangePasswordAtLogon is set to true so that the new user has this universal password once to log in, and then they must change their password after that. To change this universal password, change the text after $password = ensuring that the quotation marks stay. They will not be part of the password. For example, if my password was "insecure-password123", I would change the $password line to look like this $password = "insecure-password123"



Adding Organization Units and Domain Controllers

Next, change the variable $locationPrompt to reflect Organizational Unit(OU) and/or Domain Controller(DC) options. There are 3 options in the script, however more or less can be used. The text between the quotation marks is shown on the screen as a prompt, so put whatever you want here.

After the prompt, the if and if-else statements wait for the numbers 1,2, or 3 to be pressed, corresponding to a different OU, and setting the$location variable. Enter the full OU and DC names after the equal sign. Two OUs and one DC were given in the script, but more or less can be used. For example, if I wanted one of the option to put users in the OU "Salary" and the OU "Employee" on the DC "DC1", I would enter the following $location = "OU=Salary,OU=Employee,DC=DC1"



Other Changes

Currently, the script prompts for input both a job title and department. If these are not wanted, they can be removed. Additionally, if other data not in the script needs to be prompted, it can be added where the $title and $department. The best format is the create a variable related to the data. For example, if I want to prompt for a office phone number, I might make my variable $officephone. Don't forget that variables in Powershell start with the $. I would then follow up my variable with the prompt command and then plaintext that is shown in the terminal. Back to the office phone example, my entire line would look like this $officephone = Read-Host -Prompt "Enter an office phone number:". This will show the prompt "Enter an office phone number" and save the input as a variable named $officephone.

- AccountExpirationDate {DateTime}

- AccountNotDelegated {Boolean}

- AuthenticationPolicy {ADAuthenticationPolicy

- AuthenticationPolicySilo {ADAuthenticationPolicySilo}

- AuthType {ADAuthType}

- CannotChangePassword {Boolean}

- City {String}

- Country {String}

- Description {String}

- DisplayName {String}

- Division {String}

- EmailAddress {String}

- EmployeeID {String}

- EmployeeNumber {String}

- Enabled {Boolean}

- Fax {String}

- Initials {String}

- Manager {ADUser}

- MobilePhone {String}

- OfficePhone {String}

- Organization {String}

- OtherName {String}

- PasswordNeverExpires {Boolean}

- PasswordNotRequired {Boolean}

- POBox {String}

- PostalCode {String}

- State {String}

- StreetAddress {String}



Bringing It All Together

Now that we have all variables set, the user can be created using the parameters we have defined. This section is entirly within the NewUserParameters variable. You can see some of the variables we defined here, such as the $location and $department . If there are any variables that remain the same for every single user, they can be added here in plaintext, instead of using a variable or prompting. For example, if I want the company to be "Widgets" for every users, I would enter 'Company' = 'Widgets'. . This section also checks the existing users in the domain controller to make sure this script isn't making a duplicate.

Assuming this is option 2 or greater, the elseif statement will be required. The code will look similar to this:

} elseif ($menu_input -eq '2') {
write-host
write-host Manual option 2
#Powershell command(s) for option 2 go here
write-host
read-host Press ENTER to continue...
&$menu



Running the Script

In order to run Powershell scripts on windows, the execution policy needs to be changed. This can be done in an administrative Powershell window by running set-executionpolicy remotesigned.

Personally, I find it easier to run Powershell ISE as an administrator and then copy the command located at the start of the script and run it from there.



Finishing Up

If the script runs successfully, the message "New User has been successfully added!" will appear along with the username and password of the new user.

New Exchange Mailbox

A Powershell script that creates a new Exchange mailbox for a existing active directory user


Getting Started

Download, unzip, and open the new_mailbox.ps1 in any text editor.

Download

Changing Variables

This script requires a couple things to run correctly. I reccommend getting all of the following data first before modifying the script.

DNS or IP address of the exchange server

Full Exchange database name

Domain Controller DNS or IP address


With those three pieces, open the script and fill in the correct data to the corresponding line, without the { and } signs. Some examples are given below.

- ConnectionUri https://widgetexchange01

- database mailDB01

- DomainController DC01

How it Works

This script starts by creating a variable called $session that stores all the information needed to start a new Powershell session on the specified Exchange server. This variable is then executed and administrator credentials should be prompted.


The script will then prompt for a username of the account to create a mailbox for. This must be entered exactly as the username is in active directory. The script will then check with the domain controller specified see if that username exists, and then will create a new mailbox for that user, with their mail stored in the database specified.

Let's Connect!