How can I find the product GUID of an installed MSI setup?

For upgrade code retrieval: How can I find the Upgrade Code for an installed MSI file? (or use the html table export script – shown below in section 2) Short Version The information below has grown considerably over time and may have become a little too elaborate. How to get product codes quickly? (four approaches): … Read more

Get-Aduser -Filter will not accept a variable

There is valuable information in the existing answers, but I think a more focused summary is helpful. Note that the original form of this answer advocated strict avoidance of script blocks ({…}) and AD-provider variable evaluation, but this has been replaced with more nuanced recommendations. tl;dr Get-ADUser -Filter ‘sAMAccountName -eq $SamAc’ Do not quote the … Read more

Why should I avoid using the increase assignment operator (+=) to create a collection

Yes, the increase assignment operator (+=) should be avoided for building an object collection, see also: PowerShell scripting performance considerations. Apart from the fact that using the += operator usually requires more statements (because of the array initialization = @()) and it encourages to store the whole collection in memory rather then push it intermediately … Read more

How do I pass multiple parameters into a function in PowerShell?

Parameters in calls to functions in PowerShell (all versions) are space-separated, not comma separated. Also, the parentheses are entirely unneccessary and will cause a parse error in PowerShell 2.0 (or later) if Set-StrictMode -Version 2 or higher is active. Parenthesised arguments are used in .NET methods only. function foo($a, $b, $c) { “a: $a; b: … Read more

Renaming files by reformatting existing filenames – placeholders in replacement strings used with the -replace operator

Martin Brandl’s answer provides an elegant and effective solution, but it’s worth digging deeper: PowerShell’s -replace operator (… -replace <search>[, <replace>]): Takes a regular expression as its first operand, <search> (the search expression), and invariably matches globally, i.e., it replaces all matches. ‘bar’ -replace ‘[ra]’, ‘@’ -> ‘b@@’ If you want to replace a literal … Read more

Determine installed PowerShell version

Use $PSVersionTable.PSVersion to determine the engine version. If the variable does not exist, it is safe to assume the engine is version 1.0. Note that $Host.Version and (Get-Host).Version are not reliable – they reflect the version of the host only, not the engine. PowerGUI, PowerShellPLUS, etc. are all hosting applications, and they will set the … Read more

Need to extract value of attribute from xml from Command promt [closed]

There are literally tons of sample scripts, and docs all over the web explaining and showing how to deal with this use case… Example: https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-data-basics-xml … including discussions on this site. Though the XML snippet is not complete and you should really provide a more complete one, one can assume what it would look like. … Read more