Copy-Item: Copying Files like a Boss in PowerShell

Copying files. It’s not sexy but has to be done. In the GUI, we copy and paste with the clipboard but in PowerShell we have a cmdlet called Copy-Item. Commands for copying files have been around forever in all shell languages. In PowerShell land, the most popular way to get a copy of a file or folder in your PowerShell script from point A to point B is by using the PowerShell Copy-Item cmdlet. This cmdlet allows us to copy a file and folder while giving us the ability to recurse files in a folder, use wildcards to select the files we need to copy and even use PowerShell Remoting for a file copy! The Copy-Item cmdlet is a part of the PowerShell provider cmdlets. It’s a generic cmdlet that recognized by it’s Item noun. Most of these provider cmdlets can be used across different providers but in my nearly […]

Read more

Out-File: Using PowerShell to Redirect Output to File

Similar to a previous cmdlet of a day we covered, [Set-Content](, PowerShell also has another cmdlet called Out-File that performs a similar feat which is write output directly to a text file. Unlike the general Set-Content cmdlet that’s purpose is a universal cmdlet to create or overwrite a file at any point in a script, the Out-File PowerShell cmdlet was designed to replace the standard output redirection operator (>). Even from the DOS days, we could redirect output to a file. This cmdlet is the PowerShell way to do it. Basic Out-File Usage This cmdlet is simple to use as was the standard output redirection operator before it. Its sole purpose is to send the raw output directly to a text file with no regard. There are minimal ways to tweak how that output is written. To demonstrate, I can retrieve all of the services from a local machine which […]

Read more

Set-Content: The PowerShell Way to Write to a File

Set-Content is one of those core PowerShell cmdlets that I can’t do without. I still remember using VBscript before we could use PowerShell to write to a file. I remember always trying to remember what kind of object I needed to use and the method name. Was it FileSystemObject, FileObject or what? It was a pain! Also, even when I did recall the method name was CreateTextFile, I’d always forget to add True as the second argument. Here’s an example of the monstrosity I’m talking about.

Compare that VBScript with this PowerShell:

Which one do you prefer? I’ll take the PowerShell way! The PowerShell way uses a single cmdlet called Set-Content. This cmdlet allows us to much more easily use PowerShell to write to a file. This PowerShell cmdlet is a built-in cmdlet that has one purpose; to write to a file. It may have some parameters here […]

Read more

How to Find all NTFS Permissions For a User In a Set of Folders

Finding all NTFS Permissions For a User In a Set of Folders If you ever need to figure out which files/folders a user is explicitly defined on here’s a nice Powershell one-liner. This enumerates all files/folders in a directory, checks the ACL for each file/folder and if that ACL contains any NTFS permission for a specified user.

Read more

How to Compare the Contents of Two Folders

Have you ever needed to figure out if two folders had the same files in them? I’m not talking about the same amount of files or even files with the same name but the actual same files? A good way to do this is to generate hashes of all the files in the source directory and the destination directory and compare the two.  Let’s find out how to make this happen. Let’s say I have a folder C:\SourceFolder and C:\DestinationFolder that might have the exact same files in them. I’d like to confirm this. Creating file hashes is an expensive operation especially if the files are large so I’m going to try other checks first before I have to go to that extent. First, I can ensure that both folders just exist.

  Then, I can enumerate the files in each folder to ensure the file names are the same. […]

Read more

Use Powershell to do a Find/Replace on a Set of Text Files

Here’s how I use Powershell to search through a directory for all text files and do a find/replace on them all. This works great for me and is one of those scripts I use all the time. Simply fill in the variables with your own and let it go. I’ve only used it on directories with less than 100 text files so I make no guarantees on some jumbo-sized directories with huge text files. This script simply finds all text files in a directory then reads the content. While it reads the content it looks for the search string line by line. When it finds it, it’ll replace it while in memory. When it’s done, it overwrites the existing text file with the text file with the replaced content that it stored in memory. [wpba-attachment-list]

Read more

Use Powershell to Parse Multiple CSV Files

If you’ve got a Cisco ACS server or just have a bunch of CSV files that you’d like to parse this post is for you. I needed to find all unique instances of a MAC address in an ‘EAP Type Name’ column in a bunch of CSV files. This is the Powershell script I came up with that worked out great. This script grabs all CSV files in a directory then finds all instances of where the ‘EAP Type Name’ column in each CSV equals ‘MS-PEAP’. Based on the rows it finds, it then takes out the ‘caller-id’ column and groups them to remove all of the duplicate instances. I then sort all of these unique “caller-ids” and export that list to a CSV.

Read more

Get Free Space (On All Partitions) for a Computer

Here’s a small script I recently created to find the free hard drive space for a set of remote computers. I found multiple scripts to do this online but none that had the kind of flexibility I needed. This script allows you to choose one, multiple or all partitions on a computer and also specify how you’d like the size reported to you in KB, MB, GB or TB. It really helps me in quickly figuring out how much free hard drive space a particular computer(s) has.

Download this script on the Technet Script Repository

Read more

Multiple File Type Search Across Multiple Servers

Here’s the little script that I put together. This uses the admin share of the servers. I suspect that if remoting is used it might be faster but the script would also be more complicated for the OP to understand. I thought this was a good compromise.

Read more