PowerShell

PowerShell Reference


Activating and Using PowerShell

  1. Run Powershell as an administrator
  2. Check the policies for running scripts: Get-ExecutionPolicy -List
  3. Allow scripts to run locally for the current user: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  4. Remove permissions for scripts: Set-ExecutionPolicy Undefined -scope LocalMachine

If allowing or removing scripts for all users, use the following lines:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Undefined

Per session, per user, or per computer

Use the following variables in place of CurrentUser to modify PowerShell run policies:

  • Process (this session)
  • CurrentUser  (this user)
  • LocalMachine (all users on computer)

Set policies to allow or deny scripts – 1

  • Restricted (no scripts)
  • AllSigned (scripts from self or trusted publishers, some prompts)
  • RemoteSigned (scripts with digital signature or by self, some prompts)
  • Unrestricted (all scripts, some prompts)
  • Bypass (all scripts, no prompts)
  • Undefined (defaults to Restricted)

Run a script

  • Right click on ps1 file (Explorer)
  • .\(scriptname).ps1 (PowerShell)

Fixing encoding errors – 1, 2

  • Check current encoding: $OutputEncoding
  • Change PowerShell to UTF-8: $OutputEncoding = New-Object -typename System.Text.UTF8Encoding

Set Script Directory

  1. Create a user profile – 1, 2, 3
  • Test if user profile exists: Test-Path $profile
  • Create new profile: New-Item -path $profile -type file –force
  • Creates profile at default location: %UserProfile%\Documents\WindowsPowerShell\Micro­soft.PowerShell_profile.ps1
  1. Create a Scripts folder:  %UserProfile%\Documents\WindowsPowerShell\Scripts
  2. Edit the user profile – 1, 2:

set-location c:\     <# starts the console at c: \#>

$scripts = "$(split-path $profile)\Scripts"     <# sets the scripts folder #>
$docs    =  $(resolve-path "$Env:userprofile\Documents")
$desktop =  $(resolve-path "$Env:userprofile\Desktop")

new-item alias:np -value C:WindowsSystem32notepad.exe     <# opens notepad through 'np' #>

Clear-Host           <# clears the screen of startup actions #>
  1. Confirm the directory is working: cat variable:\scripts

 

Useful Scripts

Batch Word (Docx) to PDF

Notes:

  • Batch converts all files in a local directory – 1
  • Releases variables and closes Word background process – 1
  • Does not display a 0 in PowerShell but does in the ISE – 1
  • Outputs a message stating completion – 1
  • Overwrites existing files with similar names
  • Open Notepad, paste in text, and save as docxtopdf.ps1
$documents_path = Split-Path -parent $MyInvocation.MyCommand.Path

$word_app = New-Object -ComObject Word.Application

# This filter will find .doc as well as .docx documents
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()

}

$origpos = $host.UI.RawUI.CursorPosition
$origpos.Y += 0

$word_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word_app)
Remove-Variable word_app

$host.UI.RawUI.CursorPosition = $origpos
Write-Host "`r " -NoNewLine
Write-Host " "
Write-Host "Docx to PDF conversion completed!"
Write-Host ""

 

 

PowerShell (Large)