Power shell — это веселье

Using Copy-Item with PS Drive

Here we are creating a ps-drive and then doing the copying part. this method helps to interact with the remote location in a way that resembles working with a local drive.

This is sometimes forgotten, the easy way of accessing or checking network drives, certificates, registry, variables, etc. In this blog post, I will show you what a PSDrive does and some examples of its use.

Using Copy-Item with session

The most simple and best way. just use the ToSession parameter provided within copy-item.

in the above script, we created a PowerShell session to that remote Windows server and then we are copying the whole folder from source to destination.

Anuj Dube

Let’s say you want to copy files/folders from a local device to one or more remote Windows servers. We have lots of ways to do this but in this article, we will be looking into how to do that using PowerShell. In your local and remote at both places, you need to enable PowerShell remoting.

Step 1: Check and set the execution policy

if the exception policy is not RemoteSigned set it to RemoteSigned.

Step 2: Enable PowerShell remoting

Please keep in mind, When the SkipNetworkProfileCheck parameter is specified, it bypasses the check for network profiles during the configuration process. This allows PowerShell remoting to be enabled even if the network profile is not classified as private or domain.

Now to the main part

Using Robocopy

Robocopy (Robust File Copy) is a command-line utility built into Windows that provides advanced options for copying files and directories.

Robocopy (Robust File Copy) is a command-line utility built into Windows that provides advanced options for copying files and directories.

While using robocopy easier way is to create a local drive and then use that for copying files.

keep in mind drive name should be a single character like A, R, X etc. Not names like “newDrive”, “test” etc.

Meaning of copy options we have passed

/E: copies subdirectories, including empty ones.

/Z: enables restartable mode.

/COPYALL: copies all file information.

/R:3: specifies 3 retries on failed copies.

/W:1: sets the wait time between retries to 1 second.

Official Documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-psdrive

The Remove-PSDrive cmdlet in PowerShell is used to disconnect or remove a mapped drive (temporary drive) from the session. This cmdlet allows you to remove the drive mappings created using the New-PSDrive cmdlet.

:/>  Скачать бесплатно стандартные игры на компьютер на Windows 10 | Win10M.RU

The Remove-PSDrive cmdlet requires you to specify the drive name or the drive letter of the mapped drive that you want to remove. It will then disconnect the drive and remove it from the session.

Although the Remove-PSDrive cmdlet does not require any additional parameters, you can use the -Force parameter to forcefully remove the drive mapping, bypassing any prompts or confirmation messages.

Overall, the Remove-PSDrive cmdlet is a useful tool for managing mapped drives in PowerShell and ensuring that your session remains organized and efficient.


Method 1: Get Free Disk Space in GB

This particular example will display both the free and used disk space in GB on the C drive.

Method 2: Get Free Disk Space as Percentage

 C | @{Name="% Free Disk Space"; Expression={.Free/1GB / (.Used/1GB + $_.Free/1GB)}}

This particular example will display the percentage of free disk space on the C drive.

PowerShell get free and used disk space

  • Used disk space: 201.73 GB
  • Free disk space: 273.99 GB

PowerShell get free disk space in GB

Notice that the output displays only the free disk space in GB.

Example 2: Get Free Disk Space as Percentage Using PowerShell

 C | @{Name="% Free Disk Space"; Expression={.Free/1GB / (.Used/1GB + $_.Free/1GB)}}

PowerShell get free disk space as percentage

From the output we can see that 57.59% of the drive is free disk space.

  • Free Disk Space % = Free Space / (Used Space + Free Space)
  • Free Disk Space % = 273.99 GB / (201.73 GB + 273.99 GB)
  • Free Disk Space % = 0.5759

Note: You can find the complete documentation for Get-PSDrive in PowerShell here.

PowerShell: How to Get Folder Size in GB
PowerShell: How to Get Folder Size and File Count
PowerShell: How to List Files in Directory by Date
PowerShell: How to List All Files in Directory to Text File

enter image description here

asked Sep 9, 2023 at 11:54

deetle's user avatar

1 silver badge12 bronze badges

Building on your own discovery:

  • The System.Management.Automation.PSDriveInfo instances that Get-PSDrive outputs have two properties relating to the root path of a (file-system) drive:

        • By contrast, if you pipe to Format-List only the true Root property values are shown.
      • To see and contrast the true property values, specify the property names explicitly, via the (positionally implied) -Property parameter:

         Get-PSDrive | Format-Table Name, Root, DisplayRoot
      •  Get-PSDrive | Select-Object Name, @{ Name='TrueRoot'; Expression={ if ($_.DisplayRoot) { $_.DisplayRoot } else { $_.Root } } }
      • For system-known drive definitions (i.e. those visible to all processes, not just to PowerShell commands) as the drive letter-based path of the drive’s root directory (e.g, C:\ and Z:\) which comprise:

      • For PowerShell-only drive definitions, established with New-PSDrive (without -Persistent), it is the root path as defined (as passed to the -Root parameter), which means:

        • For PowerShell-only drives mapped to local directories, you’ll see that directory’s full, system-native path (i.e., expressed in terms of a drive known to the system):

          • E.g., for the automatically defined Temp: PowerShell drive, it is something like C:\Users\jdoe\Temp\
          • E.g., for a drive Z: established with
            New-PSDrive Z FileSystem -Root \\server\shares\dir1, you’ll see \\server\shares\dir1

answered Sep 9, 2023 at 14:05

:/>  Как убрать память зарезервирована аппаратно в windows 10

mklement0's user avatar

67 gold badges673 silver badges862 bronze badges

Some code snippets about this cmdlet from our scripts

 Set-Acl Z:\ $acl Remove-PSDrive -Name Z -Force
}
<# 
# Removing drive
Write-Host "Removing drive" -ForegroundColor $CommandInfoRemove-PSDrive -Name $UseLocalTempDriveLetter
#Stopping Transscript
Stop-Transcript 
# Removing drive
Write-Host "Removing drive" -ForegroundColor $CommandInfoRemove-PSDrive -Name $UseLocalTempDriveLetter
#Stopping Transscript
Stop-Transcript 

New-PSDrive

You can use New-PSDrive to create a map of a network drive and a Registry location. For example, to create a mapping to a location on a server, you can use New-PSDrive -Name X -PSProvider FileSystem -Root \192.168.168.166\c$ -Credential (Get-Credential) to generate an X:\ drive letter to \\192.168.166\c$ while being prompted for credentials. You can add -Persist to store it so that it’s available after rebooting again.

Power shell — это веселье

However, you can also create a PSDrive for a registry location. For example, using New-PSDrive -Name “Windows” -PSProvider “Registry” -Root “HKLM:\Software\Microsoft\Windows” will make a Windows PSDrive which you can access:

Power shell — это веселье

You can remove a drive using the Remove-PSDrive cmdlet. For example, I just created a registry and network drive, which I can remove using Remove-PSDrive X, Windows

Power shell — это веселье

More information can be found here.

What PSDrives are available?

Power shell — это веселье

The Name column shows you what you can access as a regular drive. I will detail them in the chapters below.

Alias

Using Get-Childitem Alias: will return all the aliases in your session. For example:

Power shell — это веселье

C

C (Or other physical or network-mapped drives) will be shown without the colon sign on the list. You can, of course, use Get-ChildItem C:\ to show the root contents of that drive:

Power shell — это веселье

Cert

Power shell — это веселье

Env

Using Get-ChildItem Env: will show you all the environment variables:

Power shell — это веселье

Function

Using Get-ChildItem Function: will display all the Functions in your PowerShell session. That can be your functions or ones available because a Module was loaded. (It will also show you all the drive letters even though they are not connected or mapped)

:/>  Запустить рабочий стол через командную строку и как активировать рабочий стол Windows 10 через командную строку
Power shell — это веселье

HKCU

Power shell — это веселье

— Guy Leech (@guyrleech) July 10, 2024

HKLM

Just like HKCU, you can also use Get-ChildItem HKLM: to show the contents of your System Registry. For example, using Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows will show you:

Power shell — это веселье

Temp

Power shell — это веселье

Variable

Using Get-ChildItem Variable: will show you all the variables in your current PowerShell session. Very useful for an excellent overview of your current state! For example:

Power shell — это веселье

WSMAN

Using Get-ChildItem WSMan: will show the available configuration. Using Get-ChildItem WSMan: -Recurse will show you the details per item:

Power shell — это веселье

What is a PSDrive?

Drives exposed by PowerShell providers (such as the Certificate:, Function:, and Alias: drives) and the HKLM: and HKCU: drives that are exposed by the Windows PowerShell Registry provider.

Session-specified temporary drives and persistent mapped network drives that you create by using the New-PSDrive cmdlet.”

Providers

There could be more providers available on your system other than the ones shown in the previous chapter. You can verify the ones available by running Get-PSProvider, for example:

Power shell — это веселье

More information about this here.

This Cmdlet is used in following scripts

This page was generated automatically and may contain errors. It does not claim to be complete or correct. All information without guarantee.

Wrapping up

In the chapters above, I showed you which PSDrives are available on your system and what you can do with them. It is potent to check things and easily forgotten 🙁