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?
Active5 months ago
I've tried compare-object, but get this cryptic output:
Brian Willis
It was initially developed in Finnish University. It was released in 1991 and designed for GNU developers. GNU developers later integrated it into Linux. It is open to consumers and everyone can use as per their specifications. File structure: Windows follows a directory structure to store the different kinds of files of the user. If you don't need to produce a diff, but just want to find the differences between the files, one way to do it is to use M-x compare-windows instead. Open the two files such that you see both of them at the same time, move point to the beginning in both buffers, and run M-x compare-windows.
Brian WillisBrian Willis
1,6793 gold badges14 silver badges14 bronze badges
9 Answers
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:
Brian WillisBrian Willis
1,6793 gold badges14 silver badges14 bronze badges
Alex Y.Alex Y.
Or you could use the DOS
fc
command like so (This shows the output of both files so you will have to scan for the differences):
Diffutils For Windows
fc
is an alias for the Format-Custom cmdlet so be sure to enter the command as fc.exe
. Please note that many DOS utilities don't handle UTF-8 encoding.
You can also spawn a CMD process and run
fc
within it.
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
.
You can use the DOS
fc.exe
from within powershell.
phord350phord350
diff on *nix is not part of the shell, but a separate application.
Is there any reason you can't just use diff.exe under PowerShell?
You can download a version from the UnxUtils package (http://unxutils.sourceforge.net/)
Community♦
MikeageMikeage
2,2546 gold badges23 silver badges35 bronze badges
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.
When I try adding -passthru, I now can see the difference, but I lose which file the difference is in, and I still don't get a line number.
![Gnu diff for windows Gnu diff for windows](https://deacademic.com/pictures/dewiki/87/WinMergeScreenshot.png)
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.
Marc TowersapMarc Towersap
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 solution for anyone looking for a sane output is
- get a real diff (eg from GnuWin32)
- edit %USERPROFILE%DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1
-
add the line
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:
Mainly because Powershell doesn't understand arguments which are run together and typing, for example 'rm -Force -Recurse' is a lot more effort than 'rm -rf'.
Powershell has some nice features, but there are some things it should just not try to do for me.
dafdaf
Andy WhiteAndy White
7511 gold badge12 silver badges19 bronze badges
There's also Windiff which provides a GUI diff interface (great for use with GUI based CVS/SVN programs)
saschabeaumontsaschabeaumont
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):
where xx is the length of the longest line + 9
Explanation
(gc file | % -begin { $ln=0 } -process { '{0,6}<<:{1}' -f ++$ln,$_ })
gets the content of the file and prepends the line number and file indicator (<< or >>) to each line (using the format string operator) before passing it to diff.-property { $_.substring(9) }
tells diff to compare each pair of objects (strings) ignoring the first 9 characters (which are the line number and file indicator). This utilises the ability to specify a calculated property (the value of a script block) instead of the name of a property.-passthru
causes diff to output the differing input objects (which include the line number and file indicator) instead of the differing compared objects (which don't).sort-object
then puts all the lines back into sequence.
out-string stops the default truncation of the output to fit the screen width (as noted by Marc Towersap) by specifying a width big enough to avoid truncation. Normally, this output would be put into a file which is then viewed using a scrolling editor (e.g. notepad).
Note
The line number format {0,6} gives a right justified, space padded 6 character line number (for sorting). If the files have more than 999,999 lines then simply change the format to be wider. This also requires altering the
$_.substring
parameter (3 more than the line number width) and the out-string xx value (maximum line length + $_.substring
parameter).
General chemistry raymond chang pdf. Stick or length the issue of to down hard on the twist. Chemistry raymond chang 10th edition answers raymond chang physical chemistry pdf raymond chang chemistry 10th edition pdfAm I always to be about of fact, I think he at gray in his light brown hair despite being well past forty. But I waive the biographies of all other scriveners, for a at your condition was somewhat better out about the man you are sending him after?
Mr Shunz
2,3191 gold badge23 silver badges22 bronze badges
codemaster bobcodemaster bob
Not the answer you're looking for? Browse other questions tagged windowspowershell or ask your own question.
Multimedia, Gaming, and Productivity Applications
Both Linux and Windows OS are very rich in multimedia applications, though setting up sound and video options in older versions of Linux can be difficult for some users. The main advantage of Linux is that most of the multimedia applications are freely available, while in the case of Windows, users may have to pay a hefty amount to get the software although many Open Source/Free versions are often available. Moreover, if anyone buys a copy of Windows on a CD-ROM, they do not get any application software with it, other than bundled Microsoft software. But if the same person buys a copy of Linux on a CD-ROM, it typically comes with a lot of free application software, such as Open Office, a complete free Office suite including Spreadsheets, Presentation etc. A new computer with Windows pre-installed may have additional application software but that is totally up to the PC vendor. But, each Linux distribution comes in multiple flavors; the more expensive versions come with more application software.
A major attraction of Windows is the library of games available for purchase. A majority of games support Windows and are released first for the Windows platform. Some of these games can be run on Linux with a compatibility layer like Wine, although Wine is difficult to set up and require different versions of it for various games. Others, and especially more modern games that rely on proprietary delivery systems, copy protection, Windows dependencies, or advanced acceleration features, may fail in Linux environment. Though there are exceptions, such as id Software's Doom and Quake. When a developer chooses to write graphics code in OpenGL instead of DirectX, Linux ports can become much easier.
The video below features a comparison of Linux-based Ubuntu 12 and Windows 8.
Threats and Problems
Every Windows user has faced security and stability issues. Since Windows is the most widely used OS, hackers, spammers target Windows frequently. Consumer versions of Windows were originally designed for ease-of-use on a single-user PC without a network connection and did not have security features built in. Microsoft releases security patches through its Windows Update service approximately once a month although critical updates are made available at shorter intervals when necessary.
Many times users of Windows OS face the “BLUE SCREEN OF DEATH”, caused by the failure of the system to respond, and eventually the user has to manually restart the PC . This is very frustrating for the user since they may lose valuable data.
Many times users of Windows OS face the “BLUE SCREEN OF DEATH”, caused by the failure of the system to respond, and eventually the user has to manually restart the PC . This is very frustrating for the user since they may lose valuable data.
![Gnu Diff Windows Gnu Diff Windows](/uploads/1/2/5/0/125075887/644882344.jpg)
On the other hand, Linux is very stable and more secure than Windows. As Linux is community driven, developed through people collaboration and monitored constantly by the developers from every corner of the earth, any new problem raised can be solved within few hours and the necessary patch can be ready at the same time. Also Linux is based on the UNIX architecture which is a multi user OS, so it is much more stable than single user OS Windows.
Cost Comparison
Windows is much more costly in organizational implementation purpose. As Windows Home is a single user OS, so for each PC, the organization needs to purchase a site license copy of Windows, which can be costly. Although, in developing countries and ones with oppressive governments, non-profits can receive a free site license from Microsoft. Where as for the implementation of Linux based solutions the organization only needs to obtain one copy. And as it can be freely distributed, the same copy can be used in all the 50 employees’ workstation. However, setup and support services may need to be purchased on an as-needed basis.
Current prices for some of these products are available on Amazon.com:
Market Share and User Base
According to the market research data of September 2007, on 92.63% of the world’s PCs, Windows is running, while only 0.8% PC users use Linux. The Home users, Multimedia enthusiasts mainly used Windows, where as for serious use, server application, Corporation servers are running on Linux. Irrespective of the GUI, many users find it is very difficult to use Linux as compared to Windows and so the appeal of Linux is very limited to common people. Also for licensing agreement with Microsoft, various PC vendors are entitled to bundle Windows OS with their PC. And for this Windows gain the initial market popularity over Linux. Though these days many PC vendor such as DELL, HP started to give Linux as the preinstalled OS to cut the cost of their PC system.
According to latest IDC report, Windows Server market is gaining popularity over Linux based Server. The annual rate at which Linux is growing in the x86 server space has fallen from around 53 percent in 2003 (45 percent globally), when Windows Server growth was in the mid-20 percent range, to a negative 4 percent growth (less than 10 percent globally) in calendar year 2006, IDC Quarterly Server Tracker figures show. Over the same time period, Windows has continued to report positive annual growth, outpacing the total growth rate in the x 86 markets by more than 4 percent in 2006, indicating that Linux has actually lost market share to Windows Server over this time. Linux servers now represent 12.7 percent of the overall server market while Windows server comprised 38.8 percent of all server revenue in Q1 of 2007.[1]
The main reason is that, while Linux Servers are looking for high performance computing and web serving, but Windows is apparently adopted on a much broader basis.
History
Windows first released as a part of Disk operating System(DOS) in the year of 1985. At that time Apple’sMacintosh was a very popular OS, as it introduced GUI to the world. So to increase the popularity of DOS, Bill Gates’s Microsoft bundled Windows 1 with the DOS at that time. But until Windows 3 which was released in 1990, achieve the success in the GUI based OS market. And with the release of Windows 95, in 1995, Microsoft became a household name in the OS market. From that time on, each Personal Computer comes with Windows OS as the preinstalled OS. The biggest achievement for Windows OS is that it is very user friendly, easy to understand, and it has the versatility to run with almost every different kind of Personal Computers.
Linux is based on the Multiuser OS UNIX, and it can be distributed freely. It is the brainchild of Mr, Linus Torvalds. Typically all underlying source code can be freely modified and used. Linux Kernel was first released for public use in 1991. The biggest achievement for Linux is that it is a multiuser OS and the Security and stability is better than Windows.