Get-ChildItem Fails When Using a Variable With the Include Switch - windows

Powershell v4.0
Windows 7
This code works and retrieves the 2 files I'm trying to find:
$dir = Get-Item -Path "C:\TestSource"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include *.txt,*.inf
This code works too, but it only finds the txt file:
$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter
However, this does not return any objects:
$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt,*.inf"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter
It's necessary to build the $Filter variable into an array like so:
$Dir = Get-Item -Path "C:\TestSource"
$Filter = #("*.txt","*.inf")
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter
The Microsoft page on Get-ChildItem leads me to believe it's possible to use variables with the Get-ChildItem cmdlet. However, why is the cmdlet not returning objects unless the variable is an array? Since the explicit string works in the first example, shouldn't the 3rd example work as well?

The argument to Include is always an array - in your first example -Include *.txt,*.inf passes a two-element array as the filter.
In your third example it is a comma-separated string. If you pass an array it should work:
$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt", "*.inf"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter

Lee gave the answer; in your third example you are trying to pass multiple values for the -Include parameter to evaluate but you didn't format it appropriately as an array so the script is looking for a file with this entire string pattern in its name: *.txt,*.inf

Related

Removing trailing and ending blank spaces in folder and file names on Windows in bulk

I tried following Remove leading spaces in Windows file names but it's not working for my use case.
I have a lot of folders and filenames that either have a blank space at the front or at the end. How would I go about removing those spaces in bulk?
This was the command-line command I used after following the linked post:
for /R %A IN ("* ") do #for /F "tokens=*" %B IN ("%~nxA") do #ren "%A" "%B"
But it didn't work out.
Update: thank you to all who replied trying to help. I think there is just a Windows-level glitch in the file system. I ended up just having to manually create new folders without leading and trailing spaces and then dragging all the files over manually then renaming those to non-trailing and leading names as well.
It's unclear whether or not you want a PowerShell solution, but there's a reasonable assumption to be made you might.
If you wanted a PowerShell solution, you could try this:
function Test-LeadingTrailingWhitespace {
param(
[Parameter(Mandatory)]
[String]$String
)
$String[0] -eq ' ' -Or $String[-1] -eq ' '
}
Get-ChildItem -Path "<path_to_folder>" | ForEach-Object {
if ($_.PSIsContainer -And (Test-LeadingTrailingWhitespace -String $_.Name)) {
$Destination = Split-Path -Path $_.FullName -Parent
$NewName = $_.Name.Trim()
Move-Item -Path $_ -Destination (Join-Path -Path $Destination -ChildPath $NewName)
}
elseif (Test-LeadingTrailingWhitespace -String $_.BaseName) {
$Destination = Split-Path -Path $_.FullName -Parent
$NewName = $_.BaseName.Trim() + $_.Extension
Move-Item -Path $_ -Destination (Join-Path -Path $Destination -ChildPath $NewName)
}
}
To be on the safe side, you could add -WhatIf or -Confirm on the Move-Item cmdlet. The former will tell you what would have changed without that parameter without actually making any changes (like a 'dry run'). The latter will prompt you for confirmation before making each change, giving you a chance to validate incrementally and not make changes en masse from the moment you hit enter.
Trim() is a method available for all strings in PowerShell:
Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.
You can loop over files and folder and check if they actually have a leading or trailing whitespace before renaming, this would avoid errors like:
Rename-Item: Source and destination path must be different.
We can use the -match matching operator with a simple regex ^\s|\s$ (starts with whitespace or ends with whitespace - regex101 link for a simple example) to see if the file or folder should be renamed:
Get-ChildItem path\to\startingfolder -Recurse | ForEach-Object {
$newName = switch($_) {
# handle folders
{ $_.PSIsContainer -and $_.Name -match '^\s|\s$' } {
$_.Name.Trim()
break
}
# handle files
{ $_.BaseName -match '^\s|\s$' -or $_.Extension -match '^\s|\s$' } {
$_.BaseName.Trim() + $_.Extension.Trim()
break
}
# if none of the above conditions were true, continue with next item
Default {
return
}
}
Rename-Item -LiteralPath $_.FullName -NewName $newName
}
Personally, I'd do this in two steps to rename folders and files separately. This to overcome the problem that when a folder is renamed, the items inside that folder all have a new path.
Using switch -Force allows renaming items such as hidden or read-only files
Using -ErrorAction SilentlyContinue swallows the error when the new name is equal to the existing name
$rootPath = 'X:\thepath'
# first the folders and subfolders (deepest nesting first)
(Get-ChildItem -Path $rootPath -Directory -Recurse | Sort-Object FullName -Descending) |
Rename-Item -NewName {$_.Name.Trim()} -Force -ErrorAction SilentlyContinue
# next the files
(Get-ChildItem -Path $rootPath -File -Recurse) |
Rename-Item -NewName {'{0}{1}' -f $_.BaseName.Trim(), $_.Extension.Trim()} -Force -ErrorAction SilentlyContinue

Powershell Get-ChildItem wildcard in path?

Can a wildcard be put in a folder name mask? Or is it necessary to run Get-ChildItem twice, one for the direcdtories and a second for the path? Even with the code below, I'm getting no directories returned.
I think this post shows how to do it with the older syntax:
$folderName = "c:\BizTalk\Vendors\*\AS2FilesReceived\"
$folderMask = "$folderName\*.*"
$dirs = Get-ChildItem -Path $folderName -Recurse -Directory
Write-Host "Number of Matching Directories = $($dirs.Count)"
$dirs
#$files = $dirs | %{ Get-ChildItem -Path $folderMask -Filter "*.*" -Exclude "*997*.*" -File} | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-6)} | Sort-Object LastWriteTime -Descending
Get-ChildItem supports wildcards in the path, but the wildcard applies only to a single folder level, i.e. a pattern C:\foo\*\bar will find folders C:\foo\abc\bar and C:\foo\xyz\bar, but won't find a folder C:\foo\abc\xyz\bar. For the latter you need something like this:
Get-ChildItem 'C:\foo' -Filter 'bar' -Directory -Recurse
If you have just a single variable folder level and want only files from the given path (without recursion) you can do something like this:
Get-ChildItem 'C:\foo\*\bar\*' -File
If you also want all files in subfolders below bar use this:
Get-ChildItem 'C:\foo\*\bar' -File -Recurse
Assuming that you want to find and return info about all files in all AS2FilesReceived directories for each folder under Vendors, then this might help you...
#Find all AS2FilesReceived directories
$dirs= Get-ChildItem -Path 'C:\BizTalk\Vendors\*\AS2FilesReceived\' -Directory
#Return number of directories
"$($dirs.Count) directories found"
#Return list of directory names
$dirs | % {$_.Parent.Name}
#Return the full name of every text file under each directory
$dirs | % {Get-ChildItem -Path $_ *.txt} | % {$_.FullName}
Which returns...
3 directories found
Vendor1
Vendor2
Vendor3
C:\BizTalk\Vendors\Vendor1\AS2FilesReceived\Vendor1AS2File.txt
C:\BizTalk\Vendors\Vendor2\AS2FilesReceived\Vendor2AS2File.txt
C:\BizTalk\Vendors\Vendor3\AS2FilesReceived\Vendor3AS2File.txt
Ultimately, I just wanted the files, not sure if I had a typo or what, but now this works. It's possible I swithced from $folderMask to $folderName, or didn't have the *.* on the end of the $folderMask.
$folderMask = "c:\BizTalk\Vendors\*\AS2FilesReceived\*.*"
$files = Get-ChildItem -Path $folderMask -File
Write-Host "Number of Matching Files = $($files.Count)"

How can I move items without hardcoded variables

I want to move items with the .txt extension but not like this, hard-coded. I want to select from a folder all files with some extension and move them into a folder with the same name as extension. And I want to do that for all the extensions in the directory.
Any ideas?
Thank you!
Take a look at my code but I did it with hardcoded variables
$variable=Get-ChildItem -Path "C:\somePath"
foreach ($variables in $variable)
{
if($extension=($variables | Where {$_.extension -like ".txt"}))
{
New-Item -ItemType Directory -Path "C:\somePath\text"
$extension | Move-Item -Destination "C:\somePath\text"
}
}
Although this solution is not as clean-looking as the other solution, it does handle the case where the destination folders don't already exist. It also moves files that may contain special characters like []. It also explicitly ignores files with no extension since no requirement was given for those. The amount of looping is minimized through the use of Group-Object.
$Path = "C:\Somepath"
$files = Get-ChildItem -Path $Path -File |
Group-Object -Property {($_.extension |
Select-String -Pattern "[^. ]+").matches.value
}
Foreach ($ExtGroup in $files) {
$Destination = "$Path\$($ExtGroup.Name)"
if (!(Test-Path -Path $Destination -PathType Container)) {
$null = New-Item -Path $Destination -Type Directory
}
Move-Item -LiteralPath $ExtGroup.Group -Destination $Destination -Force
}
I believe this will do:
$variable = Get-ChildItem -Path "C:\somePath"
foreach ($v in $variable){
$dest = '{0}\{1}' -f $v.DirectoryName, ($v.Extension -replace '^\.')
$v | Move-Item -Destination $dest -Force
}

Retrieve a specific field from the result of Get-ItemProperty in Powershell

I've the below powershell script:
$registrypath = "HKLM:\SYSTEM\CurrentControlSet\Control\DeviceGuard"
$Name = "EnableVirtualizationBasedSecurity"
$ExpectedValue = "1"
$value = Get-ItemProperty -Path $registrypath -Name $Name
Write-Host($value)
Its output is:
#{EnableVirtualizationBasedSecurity=1; PSPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard; PSParentPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control; PSChildName=DeviceGuard; PSDrive=HKLM;
PSProvider=Microsoft.PowerShell.Core\Registry}
I want to get the value of EnableVirtualizationBasedSecurity field in it to a variable in my powershell script.
Like $SpecificFieldValue = $value.get(EnableVirtualizationBasedSecurity);
How do i do it in powershell ?
Get-ItemProperty offers you an PSCustomObject as response.
This means you can directly get the value of the property like this:
$value.EnableVirtualizationBasedSecurity
or directly save the Value in the Get-ItemProperty-call like this:
(Get-ItemProperty -Path $registrypath -Name $Name).EnableVirtualizationBasedSecurity
or like this
Get-ItemProperty -Path $registrypath -Name $Name | Select-Object -Expandproperty EnableVirtualizationBasedSecurity
I think the problem is, that you expect the response to be an hashtable instead of an PSCustomObject.
You can get informations about the ObjectType of an Response by just adding () around a call and calling the getType() methode:
(Get-ItemProperty -Path $registrypath -Name $Name).GetType()

Powershell Rename-Item Multiple In to 1 out and case sensitivity

The following code works beautifully:
Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse | Rename-Item -NewName { $_.Name.replace("DVDRIP","DVD") }
Now, 2 questions:
Anyway I can input multiple words to replace to DVD? Here is what I was thinking:
Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse | Rename-Item -NewName { $_.Name.replace("DVDRIP|DVDR|DVD5|DVD9","DVD") }
I have tried this and other variations, but I just can't figure it out.
How can I make the code NOT case sensitive so I don't have multiple lines trying to catch the possible different spellings?
You are using a regex-pattern with a normal text-replace method. Try -replace which is made for regex. -replace is also case-insensitive by default.
Get-ChildItem -Path E:\Videos\Movies\ -File -Include *.avi,*.mp4,*.mkv -Recurse |
Rename-Item -NewName { $_.Name -replace "DVDRIP|DVDR|DVD5|DVD9","DVD" }

Resources