-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralTips.ps1
More file actions
97 lines (77 loc) · 4.97 KB
/
GeneralTips.ps1
File metadata and controls
97 lines (77 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# UNTESTED: via https://mobile.twitter.com/guyrleech/status/1566386500375351297
Set-StrictMode -version latest
# Or run through `PSScriptAnalyzer`
# List all properties on an object
X | Format-List -Property *
# Make anything an array by wrapping it in `@(X)`
@("Just one thing") # is an array of that one string
# Creating a Hashtable manually/declaritively (see also ConvertingToAndFromPSObjectAndHashtable.ps1)
# NOTE: remember to use semicolon between key-value pairs rather than C#-style comma
$x = @{ a = "asdf"; b = 3 }
# Get the length of an array piped (vs. `(X | Y).Length`)
| Measure-Object # Returns an object, though, with a Count property
| Measure-Object | % { $_.Count } # Bit of a hack, but single item arrays will output the only item as a string
# Get list of numbers to pass/pipe to something else
[System.Linq.Enumerable]::Range(0,40)
# String of length `i`, composed of `a`s (mostly just calling `new string('a', i)`)
New-Object string -ArgumentList @('a', i)
# Limit to (AKA Take) first[/top]/last/whatever X items from a previous command's enumerable
{some list} | Select-Object -First 1
{some list} | Select-Object -Last 1
{some list} | Select-Object -Skip 5 | Select-Object -First 1
# Find object in list by regex-like search (e.g., name contains "something" to find files like "somethingcool.txt")?
# `-like` is a not-quite-regex wildcard syntax
{command} | ? { $_.Name -like "*{keyword}*" }
# `-match` uses a regex syntax
{command} | ? { $_.Name -match "{keyword}" }
{command} | ? { $_.Name -match "{keyword1}.*{keyword2}.*" }
# Use percent variables like those available in cmd.exe (e.g., %USERPROFILE%)
Write-Host "${Env:USERPROFILE}"
# Access a field/property with an unusual name (e.g., contains a period or special characters in the field name itself)
# Wrap name in curly braces. (NOTE: This is not a placeholder like the `{some list}` usage above.)
$X.{Some.Property.With.Period}
# This can also apply to complex environment variable names.
${env:ProgramFiles(x86)}
# Build strings from variables.
# Basic
Write-Host "$someVar1: $someVar2"
# Complex variable names need curly braces (`${}`)
Write-Host "$someVar1: ${env:ProgramFiles(x86)}"
# Operations require parentheses (`$()`)
Write-Host "Current file path: $((Get-ChildItem GeneralTips.ps1).FullName)"
# Line breaks in strings (\n or \r\n in C# and friends)
"First line`nSecond line"
"First line`r`nSecond line"
# Edit your PowerShell profile
notepad.exe $profile # Windows Notepad
code $profile # Visual Studio Code (Windows/macOS/Linux)
nano $profile # Linux Nano
# Get user input for an environment variable
${Env:AzDOPersonalToken} = (Read-Host -Prompt "What Azure DevOps access token do you want to use?")
# Get user input as a secure string, which has special rules for how it is handled.
${Env:AzDOPersonalToken} = (Read-Host -Prompt "What Azure DevOps access token do you want to use?" -AsSecureString)
# Get user input without displaying it (e.g., password input showing **** as the user types).
${Env:AzDOPersonalToken} = (Read-Host -Prompt "What Azure DevOps access token do you want to use?" -MaskInput)
# NOTE: Environment variable names can contain characters not normally allowed in normal PowerShell variables. The curly brace syntax allows for those characters, if needed (e.g., `${Env:azdo-personal-token}`).
# Add path to user environment Path variable
$oldPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
$newPathAddition = "C:\Users\someuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts\"
# Concatenate old with addition via semicolon
$newPath = "$oldPath;$newPath"
# Save new combined path to user Path
[System.Environment]::SetEnvironmentVariable("Path", "$newPath", [System.EnvironmentVariableTarget]::User)
# Update the current path in this PowerShell session (vs. logging out and back in)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
# List all the current local variables, useful for debugging variable values at a current point in execution
Get-Variable -Scope Local
# NOTE: This appears to provide a way to get a variables name at runtime, as you would do with C#'s `nameof` operator, but that's not how this works. The value passed in to `Get-Variable` is a string, so calling with variable syntax will get the variable with the name of the underlying variable value. (If `$test="asdf"`, then `Get-Variable $test` will look for a variable named `$asdf`.):
# `(Get-Variable test).Name` # Name not validated in any way.
# Prevent line's output from showing up when running within a script.
{some command} | Out-Null;
## References
# Red Gate's PowerShell punctuation guide: https://www.red-gate.com/simple-talk/wp-content/uploads/2015/09/PSPunctuationWallChart_1_0_4.pdf
# Find location where a command is found.
(Get-Command {command-name}).Source
# This seems to work more reliably than `where`.
# Refresh environment variables after changes.
refreshenv