PowerShell: File System and Registry - Gary-Moore/developmentwiki GitHub Wiki

Providers

Windows PowerShell providers are Microsoft .NET Framework-based programs that make the data in a specialized data store available in Windows PowerShell so that you can view and manage it.

The data that a provider exposes appears in a drive, and you access the data in a path like you would on a hard disk drive. You can use any of the built-in cmdlets that the provider supports to manage the data in the provider drive. And, you can use custom cmdlets that are designed especially for the data.

The providers can also add dynamic parameters to the built-in cmdlets. These are parameters that are available only when you use the cmdlet with the provider data.

Get-PSProvider

The Get-PSProvider cmdlet gets the Windows PowerShell providers in the current session. You can get a particular drive or all drives in the session. Windows PowerShell providers let you access a variety of data stores as though they were file system drives.

Get-PSProvider

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-psprovider?view=powershell-5.1

Set-Location

The Set-Location cmdlet sets the working location to a specified location. That location could be a directory, a sub-directory, a registry location, or any provider path.

Set location to Registry HKLM (Local Machine)

Set-Location -Path "HKLM:"
PS HKLM:\>

Set location to Environment Provider

This command sets the current location to the root of the Env: drive. It uses the PassThru parameter to direct Windows PowerShell to return a PathInfo object that represents the Env: location.

Set-Location -Path "Env:" -PassThru

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Set-Location?view=powershell-5.1

Set-ItemProperty

The Set-ItemProperty cmdlet changes the value of the property of the specified item. You can use the cmdlet to establish or change the properties of items. For example, you can use Set-ItemProperty to set the value of the IsReadOnly property of a file object to $True.

You also use Set-ItemProperty to create and change registry values and data. For example, you can add a new registry entry to a key and establish or change its value.

Set a property of a file

Set-ItemProperty -Path "c:\GroupFiles\final.doc" -Name IsReadOnly -Value $True

Create a registry entry and value

Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Set-ItemProperty?view=powershell-5.1

File System

Get-ChildItem

In a file system drive, the Get-ChildItem cmdlet gets the directories, subdirectories, and files. In a file system directory, it gets subdirectories and files.

By default, Get-ChildItem gets non-hidden items, but you can use the Directory, File, Hidden, ReadOnly, and System parameters to get only items with these attributes. To create a complex attribute search, use the Attributes parameter.

Get the files and subdirectories in the current directory

Get-ChildItem

Get system files in the current directory and its subdirectories

Get-Childitem -System -File -Recurse

Get .png files in specified directory

Get-ChildItem -path C:\Path -Recurse -Include *.png

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/providers/filesystem-provider/get-childitem-for-filesystem?view=powershell-6

Copy-Item

The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it cannot copy a file to a certificate drive.

This cmdlet does not cut or delete the items being copied. The particular items that the cmdlet can copy depend on the Windows PowerShell provider that exposes the item. For instance, it can copy files and directories in a file system drive and registry keys and entries in the registry drive.

This cmdlet can copy and rename items in the same command. To rename an item, enter the new name in the value of the Destination parameter. To rename an item and not copy it, use the Rename-Item cmdlet.

Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Copy-Item?view=powershell-5.1

Move-Item

The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider. For example, it can move a file or subdirectory from one directory to another or move a registry subkey from one key to another. When you move an item, it is added to the new location and deleted from its original location.

Move-Item -Path C:\test.txt -Destination E:\Temp\tst.txt

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Move-Item?view=powershell-5.1

Rename-Item

The Rename-Item cmdlet changes the name of a specified item. This cmdlet does not affect the content of the item being renamed.

You cannot use Rename-Item to move an item, such as by specifying a path together with the new name. To move and rename an item, use the Move-Item cmdlet.

Rename-Item -Path "c:\logfiles\daily_file.txt" -NewName "monday_file.txt"

https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Rename-Item?view=powershell-5.1

icacls

Displays or modifies discretionary access control lists (DACLs) on specified files, and applies stored DACLs to files in specified directories.

icacls <FileName> [/grant[:r] <Sid>:<Perm>[...]] [/deny <Sid>:<Perm>[...]] [/remove[:g|:d]] <Sid>[...]] [/t] [/c] [/l] [/q] [/setintegritylevel <Level>:<Policy>[...]]
icacls <Directory> [/substitute <SidOld> <SidNew> [...]] [/restore <ACLfile> [/c] [/l] [/q]]

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/icacls

⚠️ **GitHub.com Fallback** ⚠️