Is there a way to make a PowerShell script work by double clicking a .ps1 file?
Create a shortcut with something like this as the “Target”: powershell.exe -command “& ‘C:\A path with spaces\MyScript.ps1’ -MyArguments blah”
Create a shortcut with something like this as the “Target”: powershell.exe -command “& ‘C:\A path with spaces\MyScript.ps1’ -MyArguments blah”
As long as the command is an executable or a file that has an associated executable, use Start-Process (available from v2): Start-Process -NoNewWindow ping google.com You can also add this as a function in your profile: function bg() {Start-Process -NoNewWindow @args} and then the invocation becomes: bg ping google.com In my opinion, Start-Job is an … Read more
Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen. The pipeline will eventually write it to out-default if nothing else uses it first. Write-Host should be used when you want to do the opposite. [console]::WriteLine is essentially what Write-Host is … Read more
The -Contains operator doesn’t do substring comparisons and the match must be on a complete string and is used to search collections. From the documentation you linked to: -Contains Description: Containment operator. Tells whether a collection of reference values includes a single test value. In the example you provided you’re working with a collection containing … Read more
The -Pattern parameter in Select-String supports an array of patterns. So the one you’re looking for is: Get-Content .\doc.txt | Select-String -Pattern (Get-Content .\regex.txt) This searches through the textfile doc.txt by using every regex(one per line) in regex.txt
If you’re going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher — I can’t explain that), and … Read more
Starting in PowerShell version 4, this is easy to do for files out of the box with the Get-FileHash cmdlet: Get-FileHash <filepath> -Algorithm MD5 This is certainly preferable since it avoids the problems the solution for older PowerShell offers as identified in the comments (uses a stream, closes it, and supports large files). If the … Read more
The given answers will only delete files (which admittedly is what is in the title of this post), but here’s some code that will first delete all of the files older than 15 days, and then recursively delete any empty directories that may have been left behind. My code also uses the -Force option to … Read more
When using the $array.Add()-method, you’re trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can’t be extended. $array += $element creates a new array with the same elements as old one + the new item, and this new larger … Read more
Just to bring Rob’s comment to light: $env:Path = [System.Environment]::GetEnvironmentVariable(“Path”,”Machine”) + “;” + [System.Environment]::GetEnvironmentVariable(“Path”,”User”)