Path to Powershell.exe (v 2.0)

I believe it’s in C:\Windows\System32\WindowsPowershell\v1.0\. In order to confuse the innocent, MS kept it in a directory labeled “v1.0”. Running this on Windows 7 and checking the version number via $Host.Version (Determine installed PowerShell version) shows it’s 2.0. Another option is type $PSVersionTable at the command prompt. If you are running v2.0, the output will be: If you’re running version … Read more

Read file line by line in PowerShell

Not much documentation on PowerShell loops. Documentation on loops in PowerShell is plentiful, and you might want to check out the following help topics: about_For, about_ForEach, about_Do, about_While. Another idiomatic PowerShell solution to your problem is to pipe the lines of the text file to the ForEach-Object cmdlet: Instead of regex matching inside the loop, you could pipe the lines through Where-Object to … Read more

Copy file remotely with PowerShell

Simply use the administrative shares to copy files between systems. It’s much easier this way. By using UNC paths instead of local filesystem paths, you help to ensure that your script is executable from any client system with access to those UNC paths. If you use local filesystem paths, then you are cornering yourself into … Read more

How to decode a Base64 string?

Isn’t encoding taking the text TO base64 and decoding taking base64 BACK to text? You seem be mixing them up here. When I decode using this online decoder I get: not the other way around. I can’t reproduce it completely in PS though. See sample below: EDIT I believe you’re using the wrong encoder for your text. The … Read more

PowerShell ‘Or’ Statement

The operator is -or, not or. See about_Logical_Operators. Also, if statements don’t read from the pipeline. Either put the if statement in a ForEach-Object loop: or use a Where-Object statement instead: And you can’t use property names by themselves. Use the current object variable ($_) to access properties of the current object. For checking if an attribute has one of a given number of values you can also … Read more

Function return value in PowerShell

PowerShell has really wacky return semantics – at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around: All output is captured, and returned The return keyword really just indicates a logical exit point Thus, the following two script blocks will do effectively the exact same … Read more

How to write to the console in PowerShell?

Default behaviour of PowerShell is just to dump everything that falls out of a pipeline without being picked up by another pipeline element or being assigned to a variable (or redirected) into Out-Host. What Out-Host does is obviously host-dependent. Just letting things fall out of the pipeline is not a substitute for Write-Host which exists for the sole reason of outputting text … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)