Powershell Scripting - eamonstackpole/my-tech-journal GitHub Wiki
Writing Scripts
- Use notepad to write scripts
- $ - used to declare variables
- foreach - for loop
-
- param ([type] $nameofvariable1, [type], $nameofvariable2) - Params
- .ps1 - file extension for PowerShell scripts
- Set-ExecutionPolicy -Scope CurrentUser RemoteSigned - allows the current user to run scripts
Running Scripts & Commands
- .\filename - runs the script
Alias
- alias - find a command alias
- Set-Alias - changes alias name
Remote PS Session
- Enter-PSSession -Computername name - creates a Powershell session for the machine
Invoke Command
- Invoke-Command -Computer Name name -ScriptBlock(command/script) - launches a command on the machine without making a session
Example: Create AD User & Add them to a Group
Create User
- New-ADUser
-
-
- -SamAccountName : Logon Name
-
-
-
-
- -AccountPassword : Password for account
-
-
- Have to convert the password string to Secure String (ConvertTo-SecureString -AsPlainText "password" -Force)
-
- -Enabled : Enables the account ($True or $False)
Add User to Group
- Add-ADGroupMember -Identity groupname -Members accountname
Notes