InWindows Explorer, just go to the Address Bar at the top (keyboard shortcuts: Alt+D or Ctrl+L) and type powershell or powershell_ise and press Enter. A PowerShell command window opens with the current directory.
As an alternative to the answer above, which requires you to type the PowerShell command (powershell.exe) each time, you can create a context menu entry just like with the "Open command window here" context menu.
So, with your favorite text editor, open a new file named OpenPowerShellHere.reg. Copy the exact text of the code above, paste it into the new file, and save it. (I would have included a copy of the file, but I couldn't figure out if attachments were possible.) If you want to exclude the command from one of the entry, just comment out the appropriate section with semicolons. My comments show you each section.
Here is my Explorer window context menu. I've highlighted the console and PowerShell commands. As you can see, you can also add a context menu entry to run an elevated command window, i.e., Run as Administrator.
Note: Context menu entries are displayed alphabetically, based on their Registry keys. The key name for the elevated command shell is "runas", which is why it comes after the PowerShell entry.
It may be set to Command Prompt by default. If so, you can change this in the Windows 10 Settings: go to Personalization -> Taskbar and enable "Replace Command Prompt with Windows PowerShell in the menu when I right-click the start button or press Windows key+X".
In Windows 10 both the command prompt and the powershell prompt can be found via the menu bar, for both non-admin and admin. These options will have its folder set to the currently selected folder from the explorer.
In the more recent versions, Windows 10 has "Open PowerShell window here" by default in the context menu when you Shift+Mouse Right Click on an empty space, and you should be using Windows Terminal by now anyway.
By adding the registry keys below, in Windows 10, I managed to get the Open PowerShell Here option in my SHIFT + RClick context menus. Just copy these into a blank notepad file then save as a .reg file and run the file to add the key and it should work from there.Some of these other answers say to add the key into HKCR\Directory\shell but I found that for me it only worked with the keys going into HKLM\SOFTWARE\Classes\Directory\shell
When you first create a shortcut to PowerShell in the usual way, the start in box specifies the home directory. If you blank out the start in box, you now have a shortcut to powershell that opens PS in the current directory, whatever that is.
My question, is there a way through PowerShell to establish a connection to the PC even if the ICMP is disabled, so that I can disable the windows Firewall, which is what I assume is causing the issue? Any suggestions is welcomed. I just dont feel like making the drive out 2 hours to disable all the PCs Defender Firewall, if I can do this remotely :).
I have two text files and want to find the differences between them using Windows Powershell. Is there something similar to the Unix diff tool available? Or is there another other way I haven't considered?
Figured it out myself. Because Powershell works with .net objects rather than text, you need to use get-content to expose the contents of the text files. So to perform what I was trying to do in the question, use:
This instructs PowerShell to start a process with the 'cmd' program using the parameters in quotes. In the quotes, is the '/c' cmd option to run the command and terminate. The actual command to run by cmd in the process is fc filea.txt fileb.txt redirecting the output to the file diff.txt.
compare-object (aka diff alias) is pathetic if you expect it to behave something like a unix diff. I tried the diff (gc file1) (gc file2), and if a line is too long, I can't see the actual diff and more importantly, I can't tell which line number the diff is on.
My advice, don't use powershell to find differences in files. As someone else noted, fc works, and works a little better than compare-object, and even better is downloading and using real tools like the unix emulator that Mikeage mentioned.
fc.exe is better for text comparing since it designed to work like *nix diff, i.e. compares lines sequentially, showing the actual differences and trying to re-synchronise (if the differing sections have different lengths). It also has some useful control options (text/binary, case sensitivity, line numbers, resynchronisation length, mismatch buffer size) and provides exit status (-1 bad syntax, 0 files same, 1 files differ, 2 file missing). Being a (very) old DOS utility, it does have a few limitations. Most notably, it does not automatically work with Unicode, treating the 0 MSB of ASCII characters as a line terminator so the file becomes a sequence of 1 character lines (@kennycoc: use the /U option to specify BOTH files are Unicode, WinXP onwards) and it also has a hard line buffer size of 128 characters (128 bytes ASCII, 256 bytes Unicode) so long lines get split up and compared separately.
compare-object is designed to determine if 2 objects are member-wise identical. if the objects are collections then they are treated as SETS (see help compare-object), i.e. UNORDERED collections without duplicates. 2 sets are equal if they have the same member items irrespective of order or duplications. This severely limits its usefulness for comparing text files for differences. Firstly, the default behaviour collects the differences until the entire object (file = array of strings) has been checked thus losing the information regarding the position of the differences and obscuring which differences are paired (and there is no concept of line number for a SET of strings). Using -synchwindow 0 will cause the differences to be emitted as they occur but stops it from trying to re-synchronise so if one file has an extra line then subsequent line comparisons can fail even though the files are otherwise identical (until there is a compensatory extra line in the other file thereby realigning the matching lines). However, powershell is extremely versatile and a useful file compare can be done by utilising this functionality, albeit at the cost of substantial complexity and with some restrictions upon the content of the files. If you need to compare text files with long (> 127 character) lines and where the lines mostly match 1:1 (some changes in lines between files but no duplications within a file such as a text listing of database records having a key field) then by adding information to each line indicating in which file it is, its position within that file and then ignoring the added information during comparison (but including it in the output) you can get a *nix diff like output as follows (alias abbreviations used):
As others have noted, if you were expecting a unix-y diff output, using the powershell diff alias would let you down hard. For one thing, you have to hold it's hand in actually reading files (with gc / get-content). For another, the difference indicator is on the right, far from the content -- it's a readability nightmare.
The -force argument is required because Powershell is quite precious about this particular inbuilt alias. If anyone is interested, having GnuWin32 installed, I also include the following in my powershell profile:
If you don't necessarily care what is different about the files and just want to know if they are different then I find that using Get-FileHash on each file and then comparing the two hashes is the most efficient way to do this. It's mostly useful when you have to compare a lot of files between two locations for differences.
PowerShell is an object-oriented automation engine and scripting language with an interactive command-line shell that Microsoft developed to help IT professionals configure systems and automate administrative tasks.
Built on the .NET framework, PowerShell works with objects, whereas most command-line shells are based on text. PowerShell is a mature and well-proven automation tool for system administrators employed in both IT departments and external entities, such as managed service providers, because of its scripting capabilities.
PowerShell originated as a proprietary offering that was only available on Windows. Today, PowerShell is available by default on most recent Windows systems; simply type "powershell" into the Windows search bar to locate the PowerShell app. In 2016, Microsoft open sourced PowerShell and made it available on Linux and macOS.
Microsoft designed PowerShell to automate system tasks, such as batch processing, and to create system management tools for commonly implemented processes. The PowerShell language, similar to Perl, offers several ways to automate tasks:
Admins can use PowerShell to handle a wide range of activities. It can extract information on OSes, such as the specific version and service pack levels. "PowerShell providers" are programs that make data contained in specialized data stores accessible at the command line. Those data stores include file system drives and Windows registries.
PowerShell also serves as the replacement for Microsoft's Command Prompt, which dates back to DOS. Microsoft, for example, made PowerShell the default command-line interface (CLI) for Windows 10 as of build 14791. PowerShell's role as a command-line shell is how most users become acquainted with the technology.
The most appealing reason to use any kind of CLI is the potential for precise and repeatable control over a desired action or task flow that is difficult, or even impossible, to replicate with a traditional GUI.
Consider using a GUI to perform an intricate task. It can involve clicking buttons, moving sliders, selecting files from multilayered menus and other actions. GUIs were designed to be comfortable for humans to use, but they can be time-consuming, cumbersome and error-prone -- especially when a task must be repeated hundreds or thousands of times.
3a8082e126