get-childitem

select second or third object / element

耗尽温柔 提交于 2019-12-08 23:54:48
问题 I want to select the second/third/forth object of a Get-ChildItem statement in my PowerShell script. This gives me the first: $first = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1 This gives me the first three: $latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 3 I would like to get the second, or the third, or the fourth. (NOT the first two and so on). Is there a way? 回答1: For selecting the n-th element

How to use Get-ChildItem with excluding a list of items with an array in Powershell?

ε祈祈猫儿з 提交于 2019-12-08 04:53:30
问题 I want to use an array for the exclusion: Remove-Item -Path "$InstallDir\Lang\*" -Exclude "de.txt", "en.txt" or Get-ChildItem "$InstallDir\Lang" -EXCLUDE "es.txt", "de.txt"| Remove-Item These both work fine. Whereas Get-ChildItem "$InstallDir\Lang\*" -Exclude "$Language" | remove-item does not work. I tried several ways ( e.g. How to use Get-ChildItem with filter array in Powershell? or How to exclude list of items from Get-ChildItem result in powershell?) but I can´t find a solution. It

move files with specific extension to folder in higher hierarchy

泄露秘密 提交于 2019-12-06 16:43:43
All my files are in specific folders: 17\1\1\PRO 17\1\2\PRO 17\2\1\PRO xx\xx\xx\PRO 17 is the year (so 18 for next year etc) the first 1 is the folder specifying the case number (can be up to 100). The second 1 is the sub parts on the case number. That last 1 has a folder PRO in it where all data resides. We need to move these files, but the files need to stay inside their respective "PRO" folders. For example: a file in 17\1\1\pro\xxx\www\ needs to go to 17\1\1\pro\movies a file in 17\2\2\pro\xxdfsdf\eeee\ needs to go to 17\2\2\pro\movies . The movies folder should get created if there are

Get-ChildItem not working with long UNC path

有些话、适合烂在心里 提交于 2019-12-06 11:39:33
问题 I try to use long UNC paths with Get-ChildItem in Powershell like Get-ChildItem -Path "\\?\c:\blabla" and Powershell says that there are illegal characters in the path. The very same path works with Resolve-Path . How can I use the "\\?\" syntax with gci ? 回答1: Great news! PowerShell v6.0.0-beta.3 and up now supports UNC paths by default ; it automatically prepends the UNC string to paths > 260 characters: https://github.com/PowerShell/PowerShell/releases/tag/v6.0.0-beta.3 https://github.com

PSRemoting performance overhead with get-childItem

南笙酒味 提交于 2019-12-05 17:16:56
This completes in 2.3 minutes on LOCALSERVER: A: measure-command {$x = invoke-command {gci -recurse "C:\"}} This completes in 38.4 minutes on LOCALSERVER: B: measure-command {$x = invoke-command -comp LOCALSERVER {gci -recurse "C:\"}} Why is B so much slower? Is it because the "output is being serialized to XML and then reconstituted into objects again", as explained here , with B but not A? Or is something else going on? LOCALSERVER runs Windows 2008R2 with PS v3. In both cases $x.count is 98973. I was wondering about changing an existing script to use PSRemoting for file searches on remote

Use PowerShell to generate a list of files and directories

醉酒当歌 提交于 2019-12-02 20:30:07
I'm writing a PowerShell script to make several directories and copy a bunch of files together to "compile" some technical documentation. I'd like to generate a manifest of the files and directories as part of the readme file, and I'd like PowerShell to do this, since I'm already working in PowerShell to do the "compiling". I've done some searching already, and it seems that I need to use the cmdlet "Get-ChildItem", but it's giving me too much data, and I'm not clear on how to format and prune out what I don't want to get my desired results. I would like an output similar to this: Directory

is get-childItem's new -file parameter fast like -filter or slow like -include?

*爱你&永不变心* 提交于 2019-12-02 09:51:18
EDIT Hoping here to clarify my convoluted and misleading question... based on my mistaken assumption that -file accepts inputs. Thanks for setting me straight and pointing out that it's just a switch parameter; the inputs in my example actually get passed to -path. Sounds like that may be the fastest purely powershell way to search for multiple file types, since -filter accepts only a single input and -include is slower. The get-childItem documentation says "Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having

select second or third object / element

孤人 提交于 2019-11-30 20:31:18
I want to select the second/third/forth object of a Get-ChildItem statement in my PowerShell script. This gives me the first: $first = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1 This gives me the first three: $latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 3 I would like to get the second, or the third, or the fourth. (NOT the first two and so on). Is there a way? For selecting the n-th element skip over the first n-1 elements: $third = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending

Powershell Get-ChildItem -Filter operates differently to Where clause with same value

吃可爱长大的小学妹 提交于 2019-11-29 13:21:26
I have a folder on a server called MyFolder. There are additional folders called MyFolder.1, MyFolder.2, MyFolder.3 etc. If I run: gci C:\Sample | ? { $_.Name -like "MyFolder.*" } I get the expected output: Directory: C:\Sample Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 16/10/2012 12:16 MyFolder.1 d---- 16/10/2012 12:16 MyFolder.2 d---- 16/10/2012 12:16 MyFolder.3 However if I run: gci C:\Sample -Filter "MyFolder.*" I get: Directory: C:\Sample Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 16/10/2012 12:16 MyFolder d---- 16/10/2012 12:16 MyFolder.1

Determine recursively both COUNT and SUM of all extensions in a folder

耗尽温柔 提交于 2019-11-29 11:28:43
I would like to be able to select a remote folder and scan it recursively for all file extensions. For each extension discovered, I would need a total count and well as the sum for individual file types. I've found a script here that works for a single file extension using the -include switch, but rather than running the script scores of times, it would be nice to simply run once and collect all extensions. $hostname=hostname $directory = "D:\foo" $FolderItems = Get-ChildItem $directory -recurse -Include *.txt $Measurement = $FolderItems | Measure-Object -property length -sum $colitems =