While sitting through Jeff Hicks' Advanced PowerShell Scripting Workshop at PowerShell on the River in Chattanooga today, he mentioned there being a "Cn" alias for the ComputerName parameter of commands in PowerShell.
I've previously written a one-liner to find parameter aliases and at one time Microsoft had starting adding parameter aliases to the help for commands as referenced in that same blog article, but it appears that they've discontinued adding them to the help and removed the ones they Read more [...]
Author: Mike F Robbins
Mike F Robbins is a Microsoft MVP on Windows PowerShell and a SAPIEN Technologies MVP. He is a co-author of Windows PowerShell TFM 4th Edition and is a contributing author of a chapter in the PowerShell Deep Dives book. Mike has written guest blog articles for the Hey, Scripting Guy! Blog, PowerShell Magazine, and PowerShell.org. He is the winner of the advanced category in the 2013 PowerShell Scripting Games. Mike is also the leader and co-founder of the Mississippi PowerShell User Group. He blogs at mikefrobbins.com and can be found on twitter @mikefrobbins.
Ever wonder how VSCode (Visual Studio Code) maintains those untitled tabs between sessions? They're stored as files underneath your user's profile in appdata on Windows based systems as shown in the following example.
The previous command could be piped to Get-Content to view the contents of all of code in the open untitled tabs of VSCode. You could also use Select-String to find something specific. I can see that three of my open tabs contain 'mikefrobbins'.
Concerned Read more [...]
1 | Get-ChildItem -Path $env:APPDATA\Code\Backups\*\untitled -Recurse |
1 2 3 | Get-ChildItem -Path $env:APPDATA\Code\Backups\*\untitled -Recurse | Select-String -SimpleMatch 'mikefrobbins' | Format-Table |
I’ll be presenting "PowerShell 101: The No-Nonsense Beginner’s Guide to PowerShell" at PowerShell on the River in Chattanooga Tennessee this Saturday, August 10th. The event itself is a mini-conference with all day sessions on Friday, August 9th and then numerous speakers presenting in three different tracks on Saturday, August 10th.
Here’s a little information about what you can expect from my session:
PowerShell 101: The No-Nonsense Beginner’s Guide to PowerShell
Interested in Read more [...]
The $PSDefaultParameterValues preference variable, which was introduced in Windows PowerShell version 3.0, provides a mechanism for specifying default values for parameters. I thought I would share what I've added to mine and ask the community to share theirs.
The first one in the list ('Out-Default:OutVariable' = 'LastResult') is one I picked up from Joel Bennett to store the results of the last command in a variable named $LastResult. Since then, I've seen Read more [...]
1 2 3 4 5 6 7 8 9 10 | $PSDefaultParameterValues = @{ 'Out-Default:OutVariable' = 'LastResult' 'Out-File:Encoding' = 'utf8' 'Export-Csv:NoTypeInformation' = $true 'ConvertTo-Csv:NoTypeInformation' = $true 'Receive-Job:Keep' = $true 'Install-Module:AllowClobber' = $true 'Install-Module:Force' = $true 'Install-Module:SkipPublisherCheck' = $true } |
I'll be presenting "PowerShell + SQL Server = Better Together" at SQL Saturday #899 in Birmingham Alabama this Saturday, July 27th. The event is free, although you need to register (some SQL Saturday's charge a small optional fee for lunch).
Here’s a little information about what you can expect from my session:
PowerShell + SQL Server = Better Together
As a SQL Server professional, are you able to make a rhyme or reason to this thing called PowerShell? Need to accomplish something? Do you Read more [...]
PowerShell version 7 is currently in preview and while it can be installed on Windows, Linux, and macOS, the examples shown in this blog article focus on installing it on a Windows based system, specifically Windows 10 using Windows PowerShell version 5 or higher which ships by default with Windows 10. Your mileage may vary with other operating systems, other versions of Windows, and/or other versions of Windows PowerShell.
The easiest way that I've found to install the preview of PowerShell version Read more [...]
I recently ran into a problem where an exported VM from Windows Server 2016 running Hyper-V wasn't able to be imported on Windows Server 2019 running Hyper-V. When attempting to perform the import, the GUI stated "No virtual machines files found" as shown in the following image. This problem may have been due to using Hyper-V manager on a remote system running Windows Server 2012 R2, although the same system was used for the export.
Since the Hyper-V servers themselves were installed with Read more [...]
Ever need to determine what generation all of the virtual machines are on your Hyper-V servers? This is simple using the Get-VM command that installs as part of the Hyper-V module for Windows PowerShell.
While the previous command will work on both clients and servers, the following command could also be used on a Windows server.
The generation of the VM is one of the properties from the results of Get-VM.
The Read more [...]
1 | Get-WindowsOptionalFeature -FeatureName *hyper*powershell -Online |
1 | Get-WindowsFeature -Name *hyper*powershell |
1 | Get-VM | Select-Object -Property Name, Generation |
When it comes to security, most people normally approach it at one extreme or the other. Some people do nothing and don't worry about it. All I can say for those folks is good luck with that and I hope your resume is updated. Others go into full blown panic mode, especially those who don't take the time to understand vulnerabilities. Many security folks and articles on the Internet don't help much either because they often blow things out of proportion which puts many of the people in the second Read more [...]
I recently worked with a vendor to remove an old application that was no longer being used from a server in one of my customer's environments. While many applications may be left to die on the server long after they're needed, this particular one transmitted data to a partner company so it definitely needed to be removed.
The problem is the application was so old that no one knew which server of the hundreds of servers it was running on. The partner company was able to provide the display name Read more [...]
Earlier this month, I presented a session on Finding Performance Bottlenecks with PowerShell at the PowerShell + DevOps Global Summit 2019 in Bellevue, Washington. The session seemed to be well received by the audience based on the feedback that I received from the attendees.
The video from this presentation is now available
https://www.youtube.com/watch?v=zaHZOffoZyE
The code and slides used during the demonstration can be found in my presentations repository on GitHub.
µ Read more [...]
Earlier this week, Chris Gardner presented a session on "Managing dependencies in PowerShell" for the Mississippi PowerShell User Group. I mentioned that I had written a function to retrieve PowerShell module dependencies that's part of my ModuleBuildTools module.
Get-MrAST is one of the primary functions that numerous other functions in the module are built on.
It retrieves the AST from one or more PS1 and/or PSM1 files, script blocks, or random arbitrary code.
It Read more [...]
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #Requires -Version 3.0 function Get-MrAst { <# .SYNOPSIS Explores the Abstract Syntax Tree (AST). .DESCRIPTION Get-MrAST is an advanced function that provides a mechanism for exploring the Abstract Syntax Tree (AST). .PARAMETER Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory. .PARAMETER Code The code to view the AST for. If Get-Content is being used to obtain the code, use its -Raw parameter otherwise the formating of the code will be lost. .PARAMETER ScriptBlock An instance of System.Management.Automation.ScriptBlock Microsoft .NET Framework type to view the AST for. .PARAMETER AstType The type of object to view the AST for. If this parameter is ommited, only the top level ScriptBlockAst is returned. .EXAMPLE Get-MrAST -Path 'C:\Scripts' -AstType FunctionDefinition .EXAMPLE Get-MrAST -Code 'function Get-PowerShellProcess {Get-Process -Name PowerShell}' .EXAMPLE Get-MrAST -ScriptBlock ([scriptblock]::Create('function Get-PowerShellProcess {Get-Process -Name PowerShell}')) .NOTES Author: Mike F Robbins Website: http://mikefrobbins.com Twitter: @mikefrobbins #> [CmdletBinding(DefaultParameterSetName='Path')] param( [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'Path', Position = 1)] [ValidateNotNull()] [Alias('FilePath')] [string[]]$Path = ('.\*.ps1', '.\*.psm1'), [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'Code')] [string[]]$Code, [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ScriptBlock')] [scriptblock[]]$ScriptBlock ) DynamicParam { $ParameterAttribute = New-Object -TypeName System.Management.Automation.ParameterAttribute $ParameterAttribute.Position = 0 $ValidationValues = Get-MrAstType $ValidateSetAttribute = New-Object -TypeName System.Management.Automation.ValidateSetAttribute($ValidationValues) $AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute] $AttributeCollection.Add($ParameterAttribute) $AttributeCollection.Add($ValidateSetAttribute) $ParameterName = 'AstType' $RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) $RuntimeParameterDictionary } BEGIN { $AstType = $PsBoundParameters[$ParameterName] } PROCESS { switch ($PSCmdlet.ParameterSetName) { 'Path' { Write-Verbose -Message 'Path Parameter Set Selected' Write-Verbose "Path contains $Path" $Files = Get-ChildItem -Path $Path -Exclude *tests.ps1, *profile.ps1 | Select-Object -ExpandProperty FullName if (-not ($Files)) { Write-Warning -Message 'No valid files found.' Return } $AST = foreach ($File in $Files) { [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$null, [ref]$null) } break } 'Code' { Write-Verbose -Message 'Code Parameter Set Selected' $AST = foreach ($c in $Code) { [System.Management.Automation.Language.Parser]::ParseInput($c, [ref]$null, [ref]$null) } break } 'ScriptBlock' { Write-Verbose -Message 'ScriptBlock Parameter Set Selected' $AST = $ScriptBlock.Ast break } default { Write-Warning -Message 'An unexpected error has occurred' } } if ($PsBoundParameters.AstType) { Write-Verbose -Message 'AstType Parameter Entered' $AST = $AST.FindAll({$args[0].GetType().Name -like "$($ASTType)Ast"}, $true) } Write-Output $AST } } |
1 | Get-MrAst -Path .\Hyper-V\MrHyperV\public\ |
A few weeks ago I was trying to figure out how to sort something in PowerShell with one property sorted descending and another one ascending. How to accomplish this was hiding in plain sight in the help examples for Sort-Object, but I thought I would documented it here for future reference.
Use the Property parameter with a hash table to specify the property names and their sort orders.
This same command can be written on one line.
It Read more [...]
1 2 3 4 5 6 7 8 | Get-Service -Name Win* | Sort-Object -Property @{ expression = 'Status' descending = $true }, @{ expression = 'DisplayName' descending = $false } |
1 | Get-Service -Name Win* | Sort-Object -Property @{expression = 'Status';descending = $true}, @{expression = 'DisplayName';descending = $false} |
I recently saw a blog article on "How to Identify Process ID for SQL Server Services? – Interview Question of the Week #185" written by Pinal Dave. While his answer is simple with TSQL, what if you're not a SQL guy? You can also retrieve this information with PowerShell from Windows itself.
When it comes to retrieving information about Windows services with PowerShell, the first command that comes to mind is Get-Service. Unfortunately, Get-Service doesn't return the process id for services. Read more [...]
I'm using VSCode for all of my PowerShell development at this point. I reloaded my system from scratch on March 13th of this year. Yesterday was the first time I've opened the PowerShell ISE since then and it was only to determine if something worked differently between the two (I tweeted this out yesterday).
One of the common problems I hear about and have experienced myself with VSCode (Visual Studio Code) is that tabbed expansion of command and parameter names doesn't work like it does in the Read more [...]
One of the ways I practice the principal of least privilege is by logging into my computer as a domain user who is also a standard local user. I typically run PowerShell as a domain user who is a local admin and elevate on a per command basis using a domain account with more access only when absolutely necessary. The problem I've run into is neither the account I'm logged into my computer as or the one I'm running PowerShell as has the ability to execute SQL queries that I need to run against various Read more [...]