functionGet-BuiltInPSVariable($Name='*') { # create a new PowerShell $ps = [PowerShell]::Create() # get all variables inside of it $null = $ps.AddScript('$null=$host;Get-Variable') $ps.Invoke() | Where-Object Name -like$Name # dispose new PowerShell $ps.Runspace.Close() $ps.Dispose() }
# create a new PowerShell $ps = [PowerShell]::Create() # get all variables inside of it $null = $ps.AddCommand('Get-Variable') $result = $ps.Invoke() # dispose new PowerShell $ps.Runspace.Close() $ps.Dispose()
classHelperStuff { # get first character of string and throw exception # when string is empty or multi-line static[char] GetFirstCharacter([string]$Text) { if ($Text.Length -eq0) { throw'String is empty' } if ($Text.Contains("`n")) { throw'String contains multiple lines' } return$Text[0] }
# get file extension in lower case static[string] GetFileExtension([string]$Path) { return [Io.Path]::GetExtension($Path).ToLower() } }
Name Value --------- Extensions [{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{0F6B957E-509E-11D1-A7CC-0000F87571E3}] Link Local Options 0 GPOLink 1 Version 65537 GPOName Guidelines of the local group lParam 0 DSPath LocalGPO FileSysPath C:\WINDOWS\System32\GroupPolicy\Machine DisplayName Guidelines of the local group
# create profile if it does not yet exist $exists = Test-Path-Path$Profile.CurrentUserAllHosts if (!$exists) { $null = New-Item-Path$Profile.CurrentUserAllHosts -ItemType File -Force }
# add code to profile @' $greetings = 'Hello there!', 'Glad to see you!', 'Happy coding!', 'Have a great day!', 'May the PowerShell be with you!' $text = $greetings | Get-Random $null = (New-Object -COM Sapi.SpVoice).Speak($text) '@ | Add-Content-Path$Profile.CurrentUserAllHosts -Encoding Default
$greetings = 'Hello there!', 'Glad to see you!', 'Happy coding!', 'Have a great day!', 'May the PowerShell be with you!'
$greetings | Get-Random
您所需要做的只是将这段代码加到您的 profile 脚本,例如这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# create profile if it does not yet exist $exists = Test-Path-Path$Profile.CurrentUserAllHosts if (!$exists) { $null = New-Item-Path$Profile.CurrentUserAllHosts -ItemType File -Force }
# add code to profile @' $greetings = 'Hello there!', 'Glad to see you!', 'Happy coding!', 'Have a great day!', 'May the PowerShell be with you!' $greetings | Get-Random '@ | Add-Content-Path$Profile.CurrentUserAllHosts -Encoding Default