Powershell

Powershell is a task automation and configuaration management framework from Micorsoft, consisting of a command-line shell and associated scripting language.

Execute PS1 Script

If you cannot execute a .ps1 script, you can run get-help about_signing.

Why? It’s owing to Window PowerShell Policy. You can get current protocol by get-executionpolicy and set it by set-executionpolicy Like:

  • get-executionpolicy - get current policy
  • Restricted (Default) - all cannot be executed
  • AllSigned - just signed can be excuted
  • RemoteSigned - just local script (even if no sign) can be excuted, but remote script
  • Unrestricted - all can be executed

Now, you can run set-executionpolicy remotesigned, then you can execute local script.

Powershell File System

Powershell File System1

$profile
# print path
C:\Users\LiuZengXi\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Now, all configurations in Microsoft.Powershell_profile.ps1 will be autoloaded.

Commands & Aliases

Alias Command Description
ni New-Item
ri, rm, rmdir, del, erase, rd Remove-Item
dir, ls, gci Get-Childitem
type, cat, gc Get-Content
gi Get-Item get specific file or directory
gp Get-ItemProperty
gl Get-Location
sp Set-ItemProperty
rni, ren Rename-Item
cd, chdir, sl Set-Location
cp, cpi Copy-Item
mi, mv, move Move-Item
Join-Path concat two path as one
Split-Path get specific part of path
Test-Path is exist?
rvpa Resolve-Path path include wildcard character

Relative Path & Absolute Path

To relative path, here four special characters:

Char Description Demo
. current directory ls .
parent directory cd
\ root directory cd \
~ home directory cd ~

And you can convert relative path to absolute path by Resolve-Path.

Save Directory Location

Command Description
Push-Location save current directory to dir-stack
Pop-Location back to a location of dir-stack
Push-Location - stack stackname save current directory to dir-stack named ‘stackname’
Pop-Location - stack stackname back to ‘stackname’

Some Special Directories

Special Directory Description Demo
Roaming Profiles $env:appdata
User profile home $env:userprofile
Application data $env:localappdata
Data used incommon $env:commonprogramfiles
Public directory $env:public
Program directory $env:programfiles
Temporary files (private) $env:tmp
Temporary files $env:temp
Windows directory dir where OS installed $env:windir

You can get all the special directories by [System.Environment + SpecialFolder].

Path Construct

Here a simple example - to create a desktop shot:

$path = [Environment]::GetFolderPath("Desktop") + "\file.txt"
$path
C:\Users\jack\Desktop\file.txt

# or more stable way
$path = Join-Path ([Environment]::GetFolderPath("Desktop")) "file.txt"
# and
$path = [System.IO.Path]::Combine([Environment]::GetFolderPath("Desktop"), "file.txt")
# in addition, you can change its info
$path = [System.IO.Path]::ChangeExtension("file.txt", "ps1") # file.ps1

Or a entire one, like:

# Create notepad.exe link
$path = [Environment]::GetFolderPath("Desktop") + "\EditorStart.lnk"
$comobject = New-Object -comObject WScript.Shell
$link = $comobject.CreateShortcut($path)
$link.targetpath = "notepad.exe"
$link.IconLocation = "notepad.exe,0"
$link.Save()
Methods Description Demo
ChangeExtension() ChangeExtension("test.txt", "ps1")
Combine() same as Join-Path Combine("C:\test", "test.txt")
GetDirectoryName() Split-Path-parent GetDirectoryName("c:\test\file.txt")
GetExtension() GetExtension("c:\test\file.txt")
GetFileName() GetFileName("c:\test\file.txt")
GetFileNameWithoutExtension() GetFileNameWithoutExtension("c:\test\file.txt")
GetFullPath() GetFullPath(".\test.txt")
GetPathRoot() Split-Path-qualifier GetPathRoot("c:\test\file.txt")
GetRandomFileName() return a random filename
GetTempFileName()
GetTempPath()
HasExtension() HasExtension("c:\test\file.txt")
IsPathRooted() Split-Path-isAbsolut IsPathRooted("c:\test\file.txt")

Footnotes

1 https://www.pstips.net/powershell-online-tutorials

ToTOP