Powershell: GetType used in PowerShell, difference between variables

First of all, you lack parentheses to call GetType. What you see is the MethodInfo describing the GetType method on [DayOfWeek]. To actually call GetType, you should do: You should see that $a is a [DayOfWeek], and $b is a custom object generated by the Select-Object cmdlet to capture only the DayOfWeek property of a data object. Hence, it’s an object … Read more

How do I force Robocopy to overwrite files?

In general, Robocopy ignores files for which lastwrittendate and filesize are the same. How can we escape this design? I’d like to force overwriting with Robocopy. I expected that dst\sample.txt should be written test001. But these file are recognized as the same files by Robocopy and not overwritten. The “/IS” option is not effective in … Read more

How to tell PowerShell to wait for each command to end before starting the next?

Normally, for internal commands PowerShell does wait before starting the next command. One exception to this rule is external Windows subsystem based EXE. The first trick is to pipeline to Out-Null like so: PowerShell will wait until the Notepad.exe process has been exited before continuing. That is nifty but kind of subtle to pick up … Read more

Difference between $? and $LastExitCode in PowerShell

$LastExitCode is the return code of native applications. $? just returns True or False depending on whether the last command (cmdlet or native) exited without error or not. For cmdlets failure usually means an exception, for native applications it’s a non-zero exit code: Cancelling a cmdlet with Ctrl+C will also count as failure; for native applications it depends on what exit code they set.

How to enter a multi-line command

You can use a space followed by the grave accent (backtick): 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: will work without problems since after the | the … Read more

What does the double asterisks mean?

Actually, I believe the above answer is wrong. Assume we have the following directory structure: dbl_wc (top level) –one_level_in –aa.txt –one_level_in1 –bb.txt –deeper_dir –abc.txt Copy-Item .\dbl_wc\**\*.txt copy_target -Force Will only look for *.txt in any directory under .\dbl_wc. And it won’t look in sub-directories (so for example .\dbl_wc\one_level_in1\deeper_dir). So it will get both aa.txt and … Read more