<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[PowerShell Explained - Medium]]></title>
        <description><![CDATA[PowerShell blog primarily focused at beginners that don’t want to just copy &amp; paste solutions, but want to understand what is going on. - Medium]]></description>
        <link>https://medium.com/powershell-explained?source=rss----1c1ab798b6c6---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>PowerShell Explained - Medium</title>
            <link>https://medium.com/powershell-explained?source=rss----1c1ab798b6c6---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 22:47:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/powershell-explained" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Parse non-Microsoft Logs in PowerShell]]></title>
            <link>https://medium.com/powershell-explained/parse-non-microsoft-logs-in-powershell-13ebb980c89b?source=rss----1c1ab798b6c6---4</link>
            <guid isPermaLink="false">https://medium.com/p/13ebb980c89b</guid>
            <category><![CDATA[logs]]></category>
            <category><![CDATA[powershell]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[parse]]></category>
            <dc:creator><![CDATA[Paul Masek]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 19:12:13 GMT</pubDate>
            <atom:updated>2018-01-05T19:12:18.232Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently needed some firewall logs for east-west traffic to hunt down some of the computers in our environment that most frequently communicate with a certain group of servers.</p><p>I was able to bug one of our network team members to get the needed CSV files from our non-Microsoft firewalls. Upon first glance at one of the CSV files in Excel, I was met with an overwhelming amount of noise to the tune of about 20,000 rows that would be an absolute bear to wade through manually.</p><p>The main item that I wanted to extract from the logs was: What are the top 10 computers that are communicating with each of these servers? Secondly, I wanted to find out their hostnames, which was a piece of information that the logs did not include.</p><p>Here’s how I went about getting the needed information.</p><p>I want to note here that the following techniques are for extracting only a couple different types of information from one type of log file, but the same principles and techniques can be applied to many different situations of parsing data in PowerShell.</p><p>I first needed to verify that the CSV file was formatted in a PowerShell friendly way, meaning that the names of the various properties in the CSV file are in the first row. If the first several rows are occupied by miscellaneous information other than log entries, than just delete all the rows above the row that includes the property names and save it as a new file. If you have the opposite situation where all you have is log entries and no property names, then I’ll give you a solution in the following example.</p><p>Once your CSV is ready, run the following command:</p><blockquote>$variableName = Import-Csv csvfilepath.csv</blockquote><p>If your CSV has no property names in the first row, you can add these with the following optional parameter, “Header”. (You can also just add them into the CSV file before you import it.)</p><blockquote>$variableName = Import-Csv csvfilepath.csv -Header “PropertyName1”, “PropertyName2”, “PropertyName3”</blockquote><p>What the previous command allowed us to do was import the data into a variable that we created, which now houses all of our log entries in a PowerShell-centric format. We can now work with this data in the same way that we would with the output from a popular command like “Get-Process”.</p><p>We can now begin parsing our data.</p><blockquote>$variableName | Select-Object -ExpandProperty “PropertyName1”</blockquote><p>You may have many different properties (columns) associated with each of your log entries (rows), but right now we want to just look at one property of each of our entries. That is the IP address. What the previous command allowed us to do is only look at the IP addresses and no other properties. This is in line with the PowerShell way of scripting and that is filter left and format right. We want to filter down to the needed data right away. This will make the script run more efficiently as all of the following commands in the pipeline won’t be bogged down by unnecessary data.</p><p>While the results are definitely slimmed down a bit, there’s still just as many of them and they are still just as difficult to wade through. Here’s where we start narrowing down our output to get to our desired outcomes — the IP addresses with the most entries and their hostnames.</p><p>There’s an extremely handy PowerShell command that will allow us to group all of the duplicate entries in our CSV file into single rows as well as list the count that each of those duplicate entries occurs. That command is “Group-Object”.</p><blockquote>$variableName | Select-Object -ExpandProperty “PropertyName1” | Group-Object |Select-Object Count, Name</blockquote><p>We’re getting much closer as we just grouped all of our duplicate log entries into single rows and we’re only selecting the needed output from the “Group-Object” command, which are the “Count” and “Name” columns.</p><p>We can now both sort the output as well as filter down to only the top 10 results with the following command.</p><blockquote>$variableName | Select-Object -ExpandProperty “PropertyName1” | Group-Object |Select-Object Count, Name | Sort-Object Count -Descending |Select-Object -First 10</blockquote><p>We just sorted our output by the “Count” property and we output the results in a descending order (largest to smallest). We then passed on those results to “Select-Object” and used the “First” parameter to only include the first 10 results, which are the IP addresses with the most entries.</p><p>The results from the last command satisfy our first goal. Now we want to look up the hostnames that go along with our 10 IP addresses. In order to do that we could use a command like Nslookup (old CMD utility) or Resolve-DNSName (new PowerShell utility) to manually lookup each of the 10 entries in 10 different commands. This would defeat the purpose of using PowerShell to automate. We can actually use a slightly modified version of the last command to pipe those results onto even the pre-PowerShell utility — Nslookup and get our desired outcome.</p><blockquote>$variableName | Select-Object -ExpandProperty “PropertyName1” | Group-Object |Select-Object Count, Name |Sort-Object Count -Descending |Select-Object -First 10 -ExpandProperty Name | Nslookup</blockquote><p>The modification (-ExpandProperty Name) extracts only the IP addresses and passes those strings onto Nslookup, which then gets us our hostnames.</p><p>Hopefully this article has given you some ideas on how you can parse logs or data in general with PowerShell.</p><p>Further Reading and References:</p><p>Using Variables to Store Objects: <a href="https://docs.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/using-variables-to-store-objects?view=powershell-5.1">https://docs.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/using-variables-to-store-objects?view=powershell-5.1</a></p><p>Import-Csv: <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-5.1">https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-5.1</a></p><p>Select-Object: <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1">https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1</a></p><p>Group-Object: <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-5.1">https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/group-object?view=powershell-5.1</a></p><p>Sort-Object: <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object?view=powershell-5.1">https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object?view=powershell-5.1</a></p><p>Nslookup: <a href="https://technet.microsoft.com/en-us/library/bb490950.aspx">https://technet.microsoft.com/en-us/library/bb490950.aspx</a></p><p>Resolve-DnsName: <a href="https://technet.microsoft.com/en-us/library/jj590781(v=wps.630).aspx">https://technet.microsoft.com/en-us/library/jj590781(v=wps.630).aspx</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13ebb980c89b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/powershell-explained/parse-non-microsoft-logs-in-powershell-13ebb980c89b">Parse non-Microsoft Logs in PowerShell</a> was originally published in <a href="https://medium.com/powershell-explained">PowerShell Explained</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get-AllAliases]]></title>
            <link>https://medium.com/powershell-explained/get-allaliases-ba9fdd2b8aaa?source=rss----1c1ab798b6c6---4</link>
            <guid isPermaLink="false">https://medium.com/p/ba9fdd2b8aaa</guid>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[efficiency]]></category>
            <category><![CDATA[powershell]]></category>
            <dc:creator><![CDATA[Paul Masek]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 19:11:50 GMT</pubDate>
            <atom:updated>2017-06-01T19:42:36.951Z</atom:updated>
            <content:encoded><![CDATA[<p>I personally find Get-Alias incredibly useful. During seasons as a sysadmin when I’m using PowerShell frequently I refer to Get-Alias both for figuring out what the full command is, as well as looking up what aliases a command has quite often. Many times though, what I’ll do is first plug in the alias name to discover the full command name (e.g. Get-Alias ls) and I’ll follow that up with discovering what other aliases that command may have are (e.g. Get-Alias -Definition Get-ChildItem). This struck me as repetitive and unnecessary. Why do two commands, when I can accomplish this same process with one?</p><p>Enter Get-AllAliases.</p><p>Get-Alias ls |ForEach-Object {Get-Alias -Definition $_.Definition}</p><p>This is a nice one-liner that is even more at home in a function placed inside a PowerShell profile like so:</p><pre>function Get-AllAliases ($Name){<br>Get-Alias $name |<br>    ForEach-Object {<br>        Get-Alias -Definition $_.Definition<br>    }<br>}</pre><p>Here’s how I went about creating this one-liner / function.</p><p>First of all, I knew that I needed the definition (full command name) of the alias, so I looked to see which property to utilize after the initial Get-Alias command is run. The following command allows me to see the full output of Get-Alias: Get-Alias | Format-List *. The output of that command looked like this:</p><pre>HelpUri             : <a href="http://go.microsoft.com/fwlink/?LinkID=113308">http://go.microsoft.com/fwlink/?LinkID=113308</a><br>ResolvedCommandName : Get-ChildItem<br>DisplayName         : gci -&gt; Get-ChildItem<br>ReferencedCommand   : Get-ChildItem<br>ResolvedCommand     : Get-ChildItem<br>Definition          : Get-ChildItem<br>Options             : ReadOnly, AllScope<br>Description         :<br>OutputType          : {System.IO.FileInfo, System.IO.DirectoryInfo}<br>Name                : gci<br>CommandType         : Alias<br>Source              :<br>Version             :<br>Visibility          : Public<br>ModuleName          :<br>Module              :<br>RemotingCapability  : PowerShell<br>Parameters          : {[Path, System.Management.Automation.ParameterMetadata], [LiteralPath,<br>                      System.Management.Automation.ParameterMetadata], [Filter, System.Management.Automation.ParameterMetadata],<br>                      [Include, System.Management.Automation.ParameterMetadata]...}<br>ParameterSets       :</pre><p>This gave me not just one, but several properties to choose from. The “Definition” property happened to be the shortest one.</p><p>So I take the “Definition” property output from Get-Alias and pipe it to ForEach-Object. ForEach-Object allows me to perform an operation on the pipeline object that I’ve chosen. Specifically the operation that I choose to perform is Get-Alias -Definition. The syntax that I use to pass on the “Definition” property is $_.Definition, $_ is the current pipeline object and .Definition specifically chooses the “Definition” property of the current pipeline object.</p><p>The function adaptation of this one-liner includes a named parameter $Name that, when ran, receives the input from the command line as a variable in the first portion of the script Get-Alias $Name.</p><p>I’ve managed to get each of these posts shorter than the last. Yes, they’re all a bit long-winded, but that’s the point of this blog. Hopefully reading this post was informative and you&#39;re moving forward with PowerShell knowledge that you didn’t have prior. Please feel free to leave any comments or critiques that you may have. I’m trying to learn PowerShell and am definitely not an expert, just a student trying to pass on knowledge to other students!</p><p>Microsoft Technet Article on PowerShell Profiles : <a href="https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx">https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba9fdd2b8aaa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/powershell-explained/get-allaliases-ba9fdd2b8aaa">Get-AllAliases</a> was originally published in <a href="https://medium.com/powershell-explained">PowerShell Explained</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Text Editor inside PowerShell]]></title>
            <link>https://medium.com/powershell-explained/text-editor-inside-powershell-22d2f5e748b8?source=rss----1c1ab798b6c6---4</link>
            <guid isPermaLink="false">https://medium.com/p/22d2f5e748b8</guid>
            <category><![CDATA[nano]]></category>
            <category><![CDATA[text-editor]]></category>
            <category><![CDATA[powershell]]></category>
            <category><![CDATA[vim]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Paul Masek]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 19:11:22 GMT</pubDate>
            <atom:updated>2019-03-28T17:09:23.706Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve ever groaned about having to leave PowerShell just to edit a file, I have good news for you.</p><p>I come from a background of having a text editor at my fingertips while doing work in the command-line. If I needed to edit a file, I would simply type “vi filename”, and I would edit the file within Vim (text editor included with most Linux systems), then exit Vim, and be right back at the command-line without skipping a beat. Normally, in order to edit a file while using PowerShell, you need to interrupt your flow by running something like “notepad filename”, which takes you back to the land of GUI’s and mice (oh the horror!), you edit your file, then go back to the command-line land of PowerShell.</p><p>Granted this problem that I have an easy solution to, is probably not a huge issue for many people. But, I think there is something to be said for streamlining your workflow, even if it’s a small adjustment (they add up). Being a systems administrator, I’m always looking for tricks and tools to automate and streamline my repetitive processes.</p><p>Without further ado, I’ll jump straight to the solution. This does require having “ Bash on Windows” installed. This is also referred to as “Bash on Ubuntu on Windows”. Essentially Bash on Windows allows you to run a Linux environment, including the Bash shell, on Windows. Read more about it here: <a href="https://msdn.microsoft.com/en-us/commandline/wsl/about">https://msdn.microsoft.com/en-us/commandline/wsl/about</a>. This site also includes installation instructions.</p><p>Now from PowerShell, all you have to do is run:</p><blockquote>bash -c “vi filename.txt”</blockquote><p>If this is your first time running Vim, you will definitely want to check out the resources at the end of this article before diving in. I’ll suffice it to say that inserting text and even exiting the program require learning. Don’t let these things discourage you from trying though, you’ll be rewarded handsomely in efficiency, if you stick with it.</p><p>If you would just like to edit a file without any learning curve, then Nano, which is another command-line text editor, may be a good choice for you. If so, run:</p><blockquote>bash -c “nano filename.txt”</blockquote><p>Something which may trip you up with Nano, is that the “^” in the keyboard shortcuts at the bottom of the screen is the “Ctrl” key. So “^O” is “Ctrl+o”. Also “^O” is labeled as “WriteOut”, this is the same as Save.</p><p>After you edit the file you’re working on, simply save it and and exit, and you’ll be right back to Powershell. You don’t need to run “bash” to go to Linux, then use Vim or Nano, then “exit” to go back to PowerShell.</p><p>This command works with both new files that you want to create and editing existing ones. For new files, it will create the file in whatever directory you are in when you run the command. For existing files, you have to use the Linux file path convention, which would look like this:</p><blockquote>bash -c “vi /mnt/c/Users/username/Documents/filename.txt”</blockquote><p>This is truly all there is to it. It’s so simple that it may even seem like it doesn’t warrant a blog post. I believe it does warrant a post though, especially for the Windows PowerShell users who have little or no Linux experience. Hopefully it opens up a new world of command-line text editors that some didn’t even know existed. For some, like myself, who have used command-line text editors, it will show them how they can be easily harnessed from PowerShell.</p><p>Going Further:</p><p>If you want to go one step further in efficiency, then Google “PowerShell profile”. Once you figure out what and where the powershell profile files are and which one you should edit, then add the following function to it and you’ll be one step further in your road to efficiency:</p><p>#Below function updated 6/22/2018 from <a href="https://medium.com/@hdformat?source=responses---------1----------------">EUNJIN LEE</a>’s comment to allow for use of tab completion ($File = $File -replace “\\”, “/”)</p><p>#Below function was updated 3/27/2019 from <a href="https://medium.com/@brendon.thiede?source=responses---------5---------------------">Brendon Thiede</a>’s comment regarding handling spaces in filenames (-replace “ “, “\ “)</p><blockquote>function vi ($File){<br>$File = $File -replace “\\”, “/” -replace “ “, “\ “<br>bash -c “vi $File”<br>}</blockquote><p>Now every time you want to edit a file you can just run:</p><blockquote>vi filename</blockquote><p>The same function and command will work with Nano as well.</p><p>Great starting point to learn Vim, this is an interactive tutorial: <a href="http://www.openvim.com/tutorial.html">http://www.openvim.com/tutorial.html</a></p><p>Vim has <strong><em>many</em></strong> keyboard shortcuts, print one of these out and put it next to your keyboard: <a href="http://michael.peopleofhonoronly.com/vim/">http://michael.peopleofhonoronly.com/vim/</a></p><p>Nice writeup on Vim, which also includes some good resources: <a href="http://hackaday.com/2016/08/08/editor-wars-the-revenge-of-vim/">http://hackaday.com/2016/08/08/editor-wars-the-revenge-of-vim/</a></p><p>Nano has a much shallower learning curve than Vim, hence only one link: <a href="https://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/">https://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22d2f5e748b8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/powershell-explained/text-editor-inside-powershell-22d2f5e748b8">Text Editor inside PowerShell</a> was originally published in <a href="https://medium.com/powershell-explained">PowerShell Explained</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ctrl-Z for Extracting a ZIP File into an Unintended Location]]></title>
            <link>https://medium.com/powershell-explained/ctrl-z-for-extracting-a-zip-file-into-an-unintended-location-97defac611a?source=rss----1c1ab798b6c6---4</link>
            <guid isPermaLink="false">https://medium.com/p/97defac611a</guid>
            <category><![CDATA[sysadmin]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[scripting]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[powershell]]></category>
            <dc:creator><![CDATA[Paul Masek]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 19:10:47 GMT</pubDate>
            <atom:updated>2018-01-05T19:10:52.827Z</atom:updated>
            <content:encoded><![CDATA[<p>I accidently extracted an archive file into a folder without creating a sub-folder for those new files to go into. And now they’re sitting alongside a whole bunch of other non-related files. This would typically mean that I would need to start over again, making sure that I dump the contents of the archive file into a sub-folder. After this, I would then need to go back up a folder level and delete all of the files I just extracted prior and make sure that I wouldn’t delete files that were already there. What a headache!</p><p>Here’s where automation in the form of PowerShell steps in.</p><p>The problem was that I dumped files into the wrong location amidst other non-related files. My solution is to first extract the archive file into it’s intended sub-folder. Secondly, delete ONLY the files that I mistakenly extracted into the parent folder.</p><p>Well the first mistake that I made, which landed me in this mess, was using the File Explorer GUI in a situation where PowerShell would have been more succinct and clear.</p><p>This time I extracted the file with the PowerShell Expand-Archive cmdlet like so:</p><blockquote>PS C:\Users\username\&gt; Expand-Archive C:\example.zip -DestinationPath C:\Example\ExampleSubFolder\</blockquote><p>Now I have two folder locations that I can compare. With an emphasis on compare, as that’s the beginning of the cmdlet we’ll use next.</p><p>I decided to use the Compare-Object cmdlet as the main workhorse of this scripted solution.</p><p>Before I get into the meat of this solution though, there needs to be a little setup first. I want to create a couple variables that will contain all of the files that need to be compared.</p><p>Let’s start with C:\Example, where I accidently dumped the files from my archive file.</p><blockquote>PS C:\Users\username&gt; $example = ls C:\Example\ -File -Force</blockquote><p>Next we’ll move onto the subfolder, which is where we intended our files to go in the first place.</p><blockquote>PS C:\Users\username&gt; $exampleSubFolder = ls C:\Example\ExampleSubFolder\ -File -Force</blockquote><p>(I should make a couple clarifications in the two above code examples. First, I have some Linux sysadmin experience, so I have a bent towards using the ls alias for the Get-ChildItem cmdlet. You can use dir too. Also this example is just directed towards files, as you can see in the -File parameter. Lastly, I made sure that I captured all files, even system and hidden ones with the -Force parameter.)</p><p>We can now compare the files in those two folders and in our comparison, we’re looking for files that are identical. Once we have the identical files, we can remove them from the parent folder, C:\Example, without disrupting the files that were already in C:\Example.</p><p>Now onto the good stuff, Compare-Object. The default usage of this cmdlet gives the results of the differences between the two objects being compared. In our case, we want to use it in the exact opposite way, which is having it give us only the files that are identical. This explains the -IncludeEqual and -ExcludeDifferent parameters I use.</p><blockquote>PS C:\Users\username&gt; Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent</blockquote><p>We’re now on the homestretch. We need to find a way to pass on these results to rm (Linux background taking over again — Remove-Item, which has a surprising amount of aliases del, erase, rd, ri, rm, rmdir), so we can remove the unwanted files from the parent folder, C:\Example.</p><p>Based on the table format output that the Compare-Object cmdlet just gave me. I can tell from the header of the files column, that I’ll need to use the InputObject property.</p><p>So I go ahead and extract the output of that InputObject property of the Compare-Object cmdlet in the following command.</p><blockquote>PS C:\Users\username&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject</blockquote><p>Don’t let the parenthesis and dot scare you. My use here of the parenthesis is a grouping expression. This allows me to grab (with the dot) the property of the output of the surrounded command.</p><p>We still have more information than we want with the output of the last command so let’s drill down one more level. Using the same idea as the previous command, we’re now going to grab the Name property of the InputObject property.</p><blockquote>PS C:\Users\username&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject.name</blockquote><p>Ahh beautiful, desired, simple, usable output that can be piped into rm, err Remove-Item.</p><p>Here’s the homestretch of the homestretch, I promise.</p><p>Go ahead and change to the parent directory where you want to remove the files using the cd command. Officially known as the Set-Location cmdlet in Powershell, cd is an alias that is actually consistent in DOS, Linux, and PowerShell.</p><blockquote>PS C:\Users\pmasek\username&gt; cd C:\Example</blockquote><p>And just to be sure (parameter -WhatIf), run:</p><blockquote>PS C:\Example&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject.name | rm -WhatIf</blockquote><p>This allows us to verify that we’re going to delete what we really want to delete.</p><p>Then Fire Away with Confidence:</p><blockquote>PS C:\Example&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject.name | rm</blockquote><p>Hopefully you’ve enjoyed this article and it’s been helpful not only in undoing an accident of extracting files to an unwanted location, but in learning how to automate with PowerShell.</p><p>As a reward for getting this far (or if you’re like me, scrolling down because you’re in a pinch and you just need answers), here’s the complete command sequence:</p><blockquote>PS C:\Users\username\&gt; Expand-Archive C:\example.zip -DestinationPath C:\Example\ExampleSubFolder\</blockquote><blockquote>PS C:\Users\username&gt; $example = ls C:\Example\ -File -Force</blockquote><blockquote>PS C:\Users\username&gt; $exampleSubFolder = ls C:\Example\ExampleSubFolder\ -File -Force</blockquote><blockquote>PS C:\Users\pmasek\username&gt; cd C:\Example</blockquote><blockquote>PS C:\Example&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject.name | rm -WhatIf</blockquote><p>Verify that you’re going to delete what you want to delete and proceed.</p><blockquote>PS C:\Example&gt; (Compare-Object -ReferenceObject $example -DifferenceObject $exampleSubFolder -IncludeEqual -ExcludeDifferent).inputobject.name | rm</blockquote><p>Cmdlet References (Most importantly Get-Help is your friend):</p><p>Expand-Archive: <a href="https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.archive/expand-archive">https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.archive/expand-archive</a></p><p>Variables: <a href="https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_variables">https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_variables</a></p><p>Get-ChildItem: <a href="https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/get-childitem">https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/get-childitem</a></p><p>Compare-Object: <a href="https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/compare-object">https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/compare-object</a></p><p>Set-Location: <a href="https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/set-location">https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/set-location</a></p><p>Remove-Item: <a href="https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.management/remove-item">https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.management/remove-item</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97defac611a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/powershell-explained/ctrl-z-for-extracting-a-zip-file-into-an-unintended-location-97defac611a">Ctrl-Z for Extracting a ZIP File into an Unintended Location</a> was originally published in <a href="https://medium.com/powershell-explained">PowerShell Explained</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>