Expand variable inside single quotes

You can use formatting and assign it to another variable: $pw = “$PsHome\powershell.exe”; $command = ‘schtasks /create /tn cleanup /tr “{0} -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1” /sc minute /mo 1’ -f $pw; cmd.exe /c $command Or you can use double quotes and escape the inside quotes with quotes: $pw = “$PsHome\powershell.exe” cmd.exe /c … Read more

Running tasks parallel in powershell

You might look into Jobs or runspaces. Here is an example of Jobs: $block = { Param([string] $file) “[Do something]” } #Remove all jobs Get-Job | Remove-Job $MaxThreads = 4 #Start the jobs. Max 4 jobs running simultaneously. foreach($file in $files){ While ($(Get-Job -state running).count -ge $MaxThreads){ Start-Sleep -Milliseconds 3 } Start-Job -Scriptblock $Block -ArgumentList … Read more

Pass function as a parameter

I’m not sure this is the best, but: function A{ Param([scriptblock]$FunctionToCall) Write-Host “I’m calling $($FunctionToCall.Invoke(4))” } function B($x){ Write-Output “Function B with $x” } Function C{ Param($x) Write-Output “Function C with $x” } PS C:\WINDOWS\system32> A -FunctionToCall $function:B I’m calling Function B with 4 PS C:\WINDOWS\system32> A -FunctionToCall $function:C I’m calling Function C with 4 … Read more

Getting “Can’t find the drive. The drive called ‘IIS’ does not exist.”

The drive is provided by the WebAdministration module, so you need to install/import that module first. How you install the module depends on your actual system and whether you use GUI or PowerShell. On a Windows Server 2008 R2 for instance you’d install the module with the following PowerShell commands: Import-Module ServerManager Add-WindowsFeature Web-Scripting-Tools After … Read more