How to handle command-line arguments in PowerShell

You are reinventing the wheel. Normal PowerShell scripts have parameters starting with -, like script.ps1 -server http://devserver Then you handle them in a param section (note that this must begin at the first non-commented line in your script). You can also assign default values to your params, read them from console if not available or … Read more

How to enter a multi-line command

You can use a space followed by the grave accent (backtick): Get-ChildItem -Recurse ` -Filter *.jpg ` | Select LastWriteTime However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be complete at that point. This includes starting a new pipeline element: … Read more

What’s the best way to determine the location of the current PowerShell script?

PowerShell 3+ # This is an automatic variable set to the current file’s/module’s directory $PSScriptRoot PowerShell 2 Prior to PowerShell 3, there was not a better way than querying the MyInvocation.MyCommand.Definition property for general scripts. I had the following line at the top of essentially every PowerShell script I had: $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

PowerShell says “execution of scripts is disabled on this system.”

If you’re using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts? As an Administrator, you can set the execution policy by typing this into your PowerShell window: Set-ExecutionPolicy RemoteSigned For … 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

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