Powershell: Difference between revisions
Jump to navigation
Jump to search
(→IO) |
|||
Line 255: | Line 255: | ||
<source lang="powershell" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
New-Item c:\foo # Create directory |
New-Item c:\foo # Create directory |
||
New-Item -Path c:\foo -ItemType Directory # Same |
New-Item -Path c:\foo -ItemType Directory # Same |
||
Remove-Item c:\foo # Remove directory |
Remove-Item c:\foo # Remove directory |
||
Test-Path c:\foo # 'True' |
Test-Path c:\foo # 'True' |
||
Copy-Item |
Copy-Item c:\foo c:\bar # Copy |
||
New-Item c:\foo\ |
New-Item c:\foo\sym -Value c:\baz -ItemType SymbolicLink -Force |
||
# Symbolic link |
# Symbolic link |
||
(gi c:\foo\sym).delete() # Remove symlink (on posh <7.1) |
|||
</source> |
</source> |
||
Revision as of 13:46, 23 February 2022
Links
- Learn powershell in Y minutes — A MUST-READ to learn rapidly.
- Everything you wanted to know about arrays - microsoft.com
- Everything you wanted to know about hashtables - microsoft.com
Reference
Files
Powershell scripts have a .ps1 extension.
Syntax
✐ | See learn Powershell in Y minutes for more! |
Primitive types / operators
Strings / arrays / hashtables / ...
# Strings
$hello = "Hello"
$world = "World"
'Hello, World!' # No interpolation
"Hello, $world!" # 'Hello, World!'
"Hello, World!".Length # Length
"{0}, {1}!" -f $hello, $world # f-string
"$world is $($world.length) char long"
'Hello, World!'[0] # 'H'
'Hello, World!'[0..5] # 'H', 'e', 'l', 'l', 'o'
'Hello, World!'[0,2,4] # 'H', 'l', 'o'
'Hello, World!'.Substring(0,5) # 'Hello'
'Hello, ' + 'World!' # 'Hello, World!'
'First line`nSecond line' # Escape with backtick
"foo" | gm # Get all methods / properties
# Arrays
# TBC
# Dictionaries
# TBC
Control flow
IO / Files
Functions
Exec
Miscellaneous
Tips
Measure execution time of a command
Measure-Command { dir }
Measure-Command { dir | Out-default} # To get output
Measure-Command { choco list }
Update help on offline computers
From MS devblogs:
<source lang="powershell">
- On online computer
New-Item c:\tmp\help Save-Help -DestinationPath c:\tmp\help -Module * -Force
- On offline computer
- ... transfer files to c:\tmp\help
- ... start powershell in admin (Win-X-A)
Update-Help -SourcePath c:\tmp\help -Module * -Force </source