Why is “

Although I’m not entirely sure that this question belongs on Stack Overflow, have you looked at the PS Cmdlet for Get-Content? Look how it’s used in the examples on TechNet in Using the Get-Content Cmdlet. Example: Get-Content c:\scripts\test.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios} Update: Above link to TechNet is broken, but mentioned in comment … Read more

What’s the difference between single quote and double quote to define a string in powershell

Double quotes allow variable expansion while single quotes do not: PS C:\Users\Administrator> $mycolor=”red” PS C:\Users\Administrator> write-output -inputobject ‘My favorite color is $mycolor’ My favorite color is $mycolor Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences (I know version 1.0 but the principle is still the same)

powershell equivalent of linux “mkdir -p”?

You can ignore errors in PowerShell with the -ErrorAction SilentlyContinue parameter (you can shorten this to -ea 0). The full PowerShell command is New-Item /foo/bar/baz -ItemType Directory -ea 0 You can shorten this to md /foo/bar/baz -ea 0 (You can also type mkdir instead of md if you prefer.) Note that PowerShell will output the … Read more

Avoid newline in echo

echo is an alias for Write-Output which sends objects to the next command in the pipeline. If all you want to do is display text on the console you can do: Write-Host “Hi” -NoNewLine Keep in mind that this is not the same cmdlet as echo|Write-Output. Write-Output‘s primary purpose is send objects to the next … Read more

Powershell – Reboot and Continue Script

There is a great article on TechNet from the Hey, Scripting Guy series that goes over a situation very similar to what you are describing: Renaming a computer and resuming the script after reboot. The magic is to use the new workflows that are part of version 3: workflow Rename-And-Reboot { param ([string]$Name) Rename-Computer -NewName … Read more

Powershell – Skip files that cannot be accessed

If you want to suppress the error message and continue executing, you need to use -ErrorAction Ignore or -ErrorAction SilentlyContinue. See Get-Help about_CommonParameters: -ErrorAction[:{Continue | Ignore | Inquire | SilentlyContinue | Stop | Suspend }] Alias: ea Determines how the cmdlet responds to a non-terminating error from the command. This parameter works only when the … Read more

Check if a path is a folder or a file in PowerShell

I found a workaround for this question: use Test-Path cmdlet with the parameter -PathType equal to Leaf for checking if it is a file or Container for checking if it is a folder: # Check if file (works with files with and without extension) Test-Path -Path ‘C:\Demo\FileWithExtension.txt’ -PathType Leaf Test-Path -Path ‘C:\Demo\FileWithoutExtension’ -PathType Leaf # … Read more