19.2. Simple Tasks with Folders and Files
This section demonstrates techniques you can use to explore drives, folders, and files on a Windows machine.
19.2.1. Finding the drives on a system
To find the drives on a machine, you use the get-psdrive cmdlet. Typically, simply typing
get-psdrive
displays all drives on the system, including exposing drives for the registry, environment variables, aliases, and functions. Figure 19-9 shows the drives on a Windows XP machine that has one hard drive.
Figure 19.9. Figure 19-9
19.2.2. Finding Folders and Files
To find folders and files use the get-childitem cmdlet and specify the Path parameter to match a single folder or file or multiple folders or files. The get-childitem cmdlet returns FileInfo or DirectoryInfo objects when used with the FileSystem provider. When files are found FileInfo objects are returned. When folders (directories) are found, then DirectoryInfo objects are returned.
If you want to find only folders you can use the following command:
get-childitem * | where-object {$_.Mode -match "d"}
Or to find files use this command:
get-childitem * | where-object {$_.Mode -notmatch "d"}
The where-object cmdlet in the second step of the pipeline looks for a match in the Mode property of the FileInfo objects or DirectoryInfo objects passed on from the first pipeline step. Figure 19-10 shows the result of executing the preceding ...
Get Professional Windows® PowerShell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.