Managing Files with PowerShell 7 on Linux
Since Microsoft open-sourced PowerShell in 2016, running it on Linux has become not only easier but a core part of the PowerShell roadmap. With PowerShell 7 in development, the stated goal is to make PowerShell accessible from not only Windows but Mac and Linux.
While PowerShell 7 is still in development, it’s a good time to go over how to do some of the most essential tasks you can do on a Linux server: managing files.
You’ll get hands-on in this article so to ensure you can follow along, be sure you have the following prerequisites in place:
Read the Best Personal and Business Tech without Ads
Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.
- A device running Linux. The examples shown in this article are run on Ubuntu 18.04.
- PowerShell 7 installed on Linux.
Understanding Bash vs PowerShell
Before you dive into the different ways PowerShell 7 can manage files, let’s first cover how Linux and PowerShell work with the Linux filesystem. Each has a different way of working with files and directories. Depending on what kind of operations you want to take, these differences can be important to know as you’ll soon find out.
For example, if you need to list files in Linux, you can either use the Bash
ls command or the PowerShell
Get-ChildItem command as you can see below. There are some slight differences that will be covered in this section, but for now, either will help you navigate around your file system.
You can see using the
ls command in the screenshot below, it’s only showing names. Contrast that with using the PowerShell
Get-ChildItem command which is showing more information like the time the items were last modified, where they are located, and what size the file is.
Bash commands only return strings while PowerShell returns an entire object. When that output is an object, it can then be filtered by using other methods on that object like using the
Where-Object command, for example.
PowerShell also gives you an easy way to verify what is being returned with the
Get-Member cmdlet. You can see below the kind of object each command produces.
PS /home/user1> Get-Item ./ThisIsAFile | Get-Member TypeName: System.IO.FileInfo --snip-- PS /home/user1> ls ./ThisIsAFile | Get-Member TypeName: System.String --snip--
Since PowerShell is returning the entire object, you have more options available to you in terms of how to query and change information of that object.
For example, suppose you wanted to get the date a file was last modified. In Bash, the easiest way to do that would be to use the
date command as shown in the screenshot below. If you want to format that string for any reason, you would have to construct how to format it as a string.
With PowerShell, the property that contains the last modified date is also a .NET
DateTime object. There is a large variety of methods available for date manipulation, including direct string formatting as shown in the screenshot below.
Managing the File System
With a better understanding of the differences between Bash and PowerShell, now you are ready to go through the different types of items in the file system.
Bash and PowerShell differ a bit in the commands requires to manage directories.
A directory is another word for folder in the Windows file ecosystem, but directory is the preferred term in the Linux lexicon. Bash and PowerShell have commands that do roughly the same operations on directories but with some differences in syntax.
Below are some of the more common operations and the Bash commands and PowerShell cmdlets to perform them:
It’s important to note that in PowerShell 7 running on a Linux device, either Bash commands or PowerShell cmdlets will run, but on a Windows device, Linux commands are aliases of the cmdlets. That means that the cmdlets will return a directory object on either platform, but using the bash command will only return an object on Windows.
Files are arguably the most important item in this list as that’s where your data and configuration live. Some of the commands that PowerShell and Bash use to operate files are similar to the ones used for directories with some different flags and options.
Below are some of the Bash commands and PowerShell cmdlets that you can use to work with files. These commands are similar to working with directories. For example,
Copy-Item still perform the same task, but there is no need to use a
-Recurse flag. If you are familiar with the directory commands, you are most of the way toward working with files.
Symlinks, or symbolic links, are files that don’t have any content of their own. Instead, they point to files that are somewhere else in on the filesystem. While not as popular in the Windows ecosystem, they are extremely popular for Linux administrators.
Symlinks are useful from referencing the same file in multiple directories to Linux executables which may be in a directory that’s not on the system path.
Fortunately, PowerShell can also handle symlinks as demonstrated in the screenshot below. In PowerShell, symlinks are treated as file objects and can be read, copied and moved like any other file. If a symlink is modified, it will make the changes on the source file. Try not to use them for write-intensive applications!
In this article, we’ve covered how Bash and PowerShell 7 treat directories, files, and symlinks differently on Linux systems. You’ve also learned how to use PowerShell cmdlets and Bash commands to manage items in the file system.
If you haven’t yet downloaded and installed PowerShell 7 and begin managing files, what are you waiting for?