Powershell Scripting - eamonstackpole/my-tech-journal GitHub Wiki

Writing Scripts

  • Use notepad to write scripts
  • $ - used to declare variables
  • foreach - for loop
    • brackets then code
  • 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
    • -name : Name of the User
    • -SamAccountName : Logon Name
    • -GivenName : First Name
    • -Surname : Last Name
    • -Path : OU Path
    • -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