Это исправление power shell для вашей проблемы с корневым сертификатом — замаскиванный вредозагрузчик

I have a folder with a lot of folders in it. I want to be able to look in the folder that has been modified today – and only copy the files with a .txt extension. I want to move them up a level. So in the picture below, I only want to search the folder that was modified today (yesterdays date in the picture), no need to check the names of the folders.

If I have a structure like this
I want the files in the folders appear directly below in the folder.
I also want to check that if there file in the folder that it is not already present in the root folder
enter image description here

Get-Childitem -Filter '*.txt' -Path "H:\FOLDERS\" -Recurse | Where lastwritetime -ge $(Get-Date).Date | Copy-Item -Destination "H:\FOLDERS\"

asked Sep 28, 2023 at 7:50

Henrik Rosqvist's user avatar

You can try the below code –

$RootFolder = "H:\Folders" #Defining the root directory
$Yesterday = (Get-Date).AddDays(-1) #Last modified date of the folder should be yesterday
$RecentFolders = Get-Childitem $RootFolder | Where-Object {$_.lastwritetime -gt $Yesterday.date}
foreach($file in $RecentFolders.FullName)
{ $Destination = Split-Path $file #Selecting one folder up $TxtFile = Get-Childitem -path $file -filter '*.txt' #Text files selected Copy-Item $TxtFile.FullName -Destination $Destination #Copy operation
}

If you want to select only those folders which are modified today, you can change the variables as

$Today = Get-Date
$RecentFolders = Get-Childitem $RootFolder | Where-Object {$_.lastwritetime -gt $Today.date}

answered Sep 28, 2023 at 9:16

Vivek Kumar Singh's user avatar

Get Current Directory or Script Path in PowerShell

As a professional working with PowerShell, I frequently receive questions to get the current directory from the script. PowerShell, Microsoft’s versatile scripting language, is integrated deeply into the Windows operating system. This makes it incredibly potent for handling file and directory manipulation tasks, making it important to know the current path you are working from. So, In this guide, we’ll walk you through the various ways to get the current directory, script directory, and script path using PowerShell. Whether you want the path of the existing script or the full path of the current location, we’ve got you covered.

:/>  Как указано в мегабайтах и ​​мегабитах и ​​битах, байтах (КБ, МБ, ГБ, ТБ). Сколько байтов в килобайте, мегабайте?

Table of contents

  • Understanding the Current Directory in PowerShell
  • How to get the current directory in PowerShell?
  • Common mistakes to avoid when working with directories in PowerShell
  • Conclusion and final thoughts

Understanding the Current Directory in PowerShell

Whether you’re dealing with files, making configuration changes, or working with scripts, being cognizant of your current directory is critical. The current directory, or working directory, is the directory (folder) in which your PowerShell environment is currently operating. It defines the point from which relative paths originate and plays a crucial role in file management tasks.

Method 1: Using the Get-Location Cmdlet

The most direct way to obtain your current directory in PowerShell is by employing the Get-Location cmdlet. This cmdlet retrieves the current working location, i.e., the path of the directory you’re in. This cmdlet returns the Path of the current directory, including the path and the provider (file system or registry). To use it, type “Get-Location” and press Enter. The system will display the full path of the current directory in the PowerShell prompt.

The syntax for the Get-Location cmdlet goes like this:

#Syntax 1
Get-Location [-PSProvider <String[]>] [-PSDrive <String[]>] [<CommonParameters>]
#Syntax 2
Get-Location [-Stack] [-StackName <String[]>] [<CommonParameters>]

Here is the list of nifty parameters of this cmdlet.

ParameterDescriptionExample
-PSDriveRetrieves the current location from the specified PowerShell drive. E.g., PowerShell Registry Provider.Get-Location -PSDrive HKCU
-PSProviderRetrieves the current location for the specified provider. Providers can include the FileSystem, Registry, Certificate stores, etc. If the provider supports multiple drives, this cmdlet returns the location of the most recently accessed drive.Get-Location -PSProvider FileSystem
-StackDisplays the location default stack. PowerShell maintains a stack of locations you can push to and pop from using Push-Location cmdlet and Pop-Location cmdlet. This parameter shows the current stack contents.Get-Location -Stack
-StackNameThe Stackname parameter displays the location from a named location stack. You can have multiple named location stacks.Get-Location -StackName myStack

It can move between PowerShell drives, different drives, Windows directories, etc. Here’s an example to get the current directory path using the Get-Location command:

PS C:\Users\Thomas> Get-Location
Path
----
C:\Users\Thomas
PS C:\Users\Thomas> pwd
Path
----
C:\Users\Thomas

When you run this command, PowerShell returns the path of your current directory. This serves as your springboard, the starting point from which all relative paths are defined.

:/>  Изоляция графов аудиоустройств windows что это как удалить

Method 2: Getting the script directory in PowerShell using $PSScriptRoot variable

The script directory is the directory where the currently running script is located. This is useful for accessing other files or scripts in the same directory as the script. To get the script directory in PowerShell, you can use the $PSScriptRoot variable. This variable contains the full path of the directory where the script is located. Here’s an example: Say you have a PowerShell script file called “CopyFiles.ps1” with the below content:

Executing the script retrieves the current location where the script is stored.

PS C:\Users\Thomas\Desktop> .\CopyFiles.ps1
C:\Users\Thomas\Desktop

Method 3: Getting the script path in PowerShell

The script path is the full path of the currently running script, including the drive letter and any subdirectories. To get the script path in PowerShell, you can use the $MyInvocation variable. This variable contains information about the current script, including the path and the command line arguments. Here’s an example:

#Get Script Path
$MyInvocation.MyCommand.Path
#Get the folder of the script
Split-Path -Path $MyInvocation.MyCommand.Path -Parent

Here is what you’ll see when running the script:

PS C:\Users\Thomas\Desktop> .\MyScript.ps1
C:\Users\Thomas\Desktop\MyScript.ps1
C:\Users\Thomas\Desktop

Method 4: Using the $PWD Variable

Another way to get the current directory in PowerShell is by using the $PWD variable. This variable holds the current working directory as a PowerShell object. To display the current directory using this method, type “$PWD” and press Enter. The full path of the current directory will be displayed.

$pwd | Select -ExpandProperty Path

Method 5: Using the [Environment]::CurrentDirectory Property

[Environment]::CurrentDirectory 
get current directory in powershell

Method 6: Get the current directory path using Get-Item cmdlet

Here, the dot (.) specifies the current directory.

:/>  Как переместить папку Рабочий стол

Common mistakes to avoid when working with directories in PowerShell

Here are some common mistakes to avoid when working with directories in PowerShell:

  1. Assuming that the current directory is always the same. The current directory can be changed by scripts, cmdlets, or user input.
  2. Using relative paths without considering the current directory. Relative paths are resolved relative to the current directory, so changing the current directory can affect the results.
  3. We often forget to handle errors when accessing files or directories. PowerShell provides many error handling mechanisms, such as the Try-Catch-Finally statement and the -ErrorAction parameter.

Conclusion and final thoughts

In conclusion, there are several ways to get the current directory in PowerShell, ranging from simple commands like pwd and Get-Location to using variables and properties. In this guide, I have explained the concept of the current directory in PowerShell and shown you how to get the current directory, script directory, and script path using PowerShell. Whether you need the full path or script directory, it’s quick and easy to get in PowerShell. Experiment with these methods to find the best one for your needs.

To change the location to a different path, use the Set-Location cmdlet (E.g., Set-Location C:\Temp). More here: How to Change the current Directory in PowerShell?

How can I retrieve the current directory in PowerShell?

To retrieve the current directory in PowerShell, you can use the PowerShell cmdlets like “Get-Location” or its alias pwd. For example:
$currentDirectory = Get-Location
Write-Host "Current directory: $($currentDirectory.Path)"

How do I get the path of the current script in PowerShell?

To get the path of the current script in PowerShell, you can use the “$MyInvocation.MyCommand.Path” command.

How can I access a specific folder in PowerShell?

How do I find the path of a folder in PowerShell?

In PowerShell, you can find the path of a folder using Resolve-Path for Relative Paths:
$RelativePath = ".\Logs"
$FullPath = Resolve-Path $RelativePath
Write-Output $FullPath

How can I check if a specific directory is the current directory?

How do I get the parent directory of the current directory?