Hope you can help me with a problem trying to execute a script block
Get-AzAksVersion -Location CanadaCentral | Where-Object -Property OrchestratorVersion -gt 1.22.11, IsPreview -ne $true
trying to get the versions greater than the current version and excluding the preview version, getting error as Parameter set cannot be resolved. Thanks in advance.
I tried to reproduce the same in my environment and got the same error as below:
Get-AzAksVersion -Location CanadaCentral | Where-Object -Property OrchestratorVersion -gt 1.22.11, IsPreview -ne $true
To resolve the error, try the below command:
Get-AzAksVersion -Location CanadaCentral | where-Object {($_.OrchestratorVersion -gt '1.22.15') -and ($_.IsPreview -ne 'True')}
I have a folder called "Testnames" and within that two folders called "Bunny, Bugs_" and "Lightyear, Buzz".
Using the command:
Get-ChildItem C:\Users\Galen\Desktop\Testnames -Recurse -Exclude '_'
I get:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 10/9/2015 11:43 AM Lightyear, Buzz
Perfect!
However when I use the command:
Copy-Item C:\Users\Galen\Desktop\Testnames -Exclude '_' -Recurse C:\Users\Galen\Testfolder
Both Bug's and Buzz's folders are copied into Testfolder. What change do I need to implement to only copy Buzz's folder?
Run Get-ChildItem without the parameter -Recurse to get the root folders. Filter that list for items that don't have an underscore in their name. Then copy the remaining items recursively.
Get-ChildItem 'C:\Users\Galen\Desktop\Testnames' |
Where-Object { $_.Name -notlike '*_*' } |
Copy-Item -Destination 'C:\Users\Galen\Testfolder' -Recurse
So, I want to write a powershell command that lists all the PIDS of all services within a range of numbers. So, it would display only pids greater than 5 but less than 35.
Get-Process | where-object {$_.WorkingSet -gt 5} | where-object {$_.WorkingSet -lt 35}
I am not sure the -lt is even valid to be honest- but can someone at least tell me if I am close or on the right path?
Close. I'd do this:
Get-Process | where-object {$_.WorkingSet -gt 5 -and $_.WorkingSet -lt 35}
I am trying to copy 7 random .txt files to a different location, but sub-folders get copied instead of the .txt files.
Here is my script:
$d = #(gci G:\Users\Public\Test) | resolve-path | get-random -count 2
$d | gci | get-random -count 7
Copy-Item $d -destination G:\Users\Public\Videos
What do I need to change?
One possible solution might be to use the PSIsContainer attribute to filter out folders.
I tried the following...
$d = gci "C:\Work\a\*.txt" | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7
Copy-Item $d -destination C:\Work\b
The where clause filtered out anything that was not a "container" and ignored the test folders I had set up. If you need .txt files specifically then use the wildcard included in the path as above.
Also, if you were to add -recurse then it would presumably search in all sub folders of your original search location and still filter out any "folders" for copying. Though I haven't tested this very thoroughly.
$d = gci "C:\Work\a\*.txt" -recurse | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7
I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (c:\MyFolder). Here's what I've done:
Write-Host ( Get-ChildItem c:\MyFolder ).Count;
The problem is, that if I have 1 or 0 items, the command does not work---it returns nothing.
Any ideas?
You should use Measure-Object to count things. In this case it would look like:
Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count;
or if that's too long
Write-Host ( dir c:\MyFolder | measure).Count;
and in PowerShell 4.0 use the measure alias instead of mo
Write-Host (dir c:\MyFolder | measure).Count;
I finally found this link:
https://blogs.perficient.com/microsoft/2011/06/powershell-count-property-returns-nothing/
Well, it turns out that this is a quirk caused precisely because there
was only one file in the directory. Some searching revealed that in
this case, PowerShell returns a scalar object instead of an array.
This object doesn’t have a count property, so there isn’t anything to
retrieve.
The solution -- force PowerShell to return an array with the # symbol:
Write-Host #( Get-ChildItem c:\MyFolder ).Count;
If you need to speed up the process (for example counting 30k or more files) then I would go with something like this..
$filepath = "c:\MyFolder"
$filetype = "*.txt"
$file_count = [System.IO.Directory]::GetFiles("$filepath", "$filetype").Count
Only Files
Get-ChildItem D:\ -Recurse -File | Measure-Object | %{$_.Count}
Only Folders
Get-ChildItem D:\ -Recurse -Directory | Measure-Object | %{$_.Count}
Both
Get-ChildItem D:\ -Recurse | Measure-Object | %{$_.Count}
You can also use an alias
(ls).Count
Recursively count files in directories in PowerShell 2.0
ls -rec | ? {$_.mode -match 'd'} | select FullName, #{N='Count';E={(ls $_.FullName | measure).Count}}
In powershell you can to use severals commands, for looking for this commands digit: Get-Alias;
So the cammands the can to use are:
write-host (ls MydirectoryName).Count
or
write-host (dir MydirectoryName).Count
or
write-host (Get-ChildrenItem MydirectoryName).Count
To count the number of a specific filetype in a folder.
The example is to count mp3 files on F: drive.
( Get-ChildItme F: -Filter *.mp3 - Recurse | measure ).Count
Tested in 6.2.3, but should work >4.