I need to do a condition in Power Shell - powershell

I'm searching a version of some files by Power Shell, example:
Get-ChildItem "C:\*\packetbeat.exe" -Recurse |
Select-Object -ExpandProperty VersionInfo |
Select-Object -ExpandProperty ProductVersion
And it shows me:
7.8.0
But I need to wait for the command finish its function, in this case the command will find in all disk C
I need finish this task when the first match appears.

If you want the search to stop after the first match, you can use this:
Get-ChildItem "C:\*\packetbeat.exe" -Recurse |
Select-Object -First 1 |
Select-Object -ExpandProperty VersionInfo |
Select-Object -ExpandProperty ProductVersion

Related

Sort folders according last modified date of subfolders

I have a windows network share with over 1000 folders that also have subfolders.
I want to delete the folders that have not been modified since a few years.
When I sort in File Explorer after "date modified", it sorts the parent folders correctly.
But changes in subfolders are not considered.
I try to create a PowerShell script that sorts all folders on the network share considering the last write time in folder or subfolder.
Unfortunately in the current state it lists only the latest modified folder over and over again.
Example:
Get-ChildItem c:\tmp -Directory | ForEach-Object { Get-ChildItem -Directory -Recurse | Sort {$_.LastWriteTime} -Descending | Select {$_.FullName}, {$_.LastWriteTime} -First 1 }
Can someone help?
I think you want "the most recent LastWriteTime of any sub-file/folder", which is
Get-ChildItem -Recurse -Force |
Sort-Object -Property LastWriteTime |
Select-Object -Last 1 -ExpandProperty LastWriteTime
Then you could write the whole thing like:
Get-ChildItem -Path 'c:\test' -Directory |
Select-Object -Property Name, #{Label='LastWriteInside'; Expression={
($_ | Get-ChildItem -Recurse -Force|
Sort-object -property LastWriteTime|
Select-Object -expandproperty lastwritetime -Last 1)
}} |
Sort-Object -Property LastWriteInside

How to get a program's installation path using Powershell

I've tried to use Get-ChildItem to get installed program property information and it does provide some of the information I require but the Installed Location/path is usually blank. Given a program's name/displayname, is there a way reliable way to get the installation path of a Windows Server program (remote to other servers) using Powershell?
Thanks in advance.
Using Registry:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
% { Get-ItemProperty $_.PsPath } | Select DisplayName,InstallLocation
Using WMI:
Get-WmiObject -Class Win32_Product -Filter 'Name like "%Microsoft Office%"' |
Select Caption,InstallLocation
For Remoting, Through registry it's totally different story, with WMI just add the -ComputerName Parameter (and make sure you have permissions)
These 2 cmdlets:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | % { Get-ItemProperty $_.PsPath } | Select DisplayName,InstallLocation | Sort-Object Displayname -Descending
Get-ChildItem HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | % { Get-ItemProperty $_.PsPath } | Select DisplayName,InstallLocation | Sort-Object Displayname -Descending
These show different programs and their locations

Return the second level directory names only

I would like to retrieve all (and only) second level directory names of my disk. For example, C:\folder1\folder2 and C:\folder1\folder3, I need to retrieve only folder2 and folder3.
I write this and the PS displays all the directory names:
Get-ChildItem -Recurse | ?{ $_.PSIsContainer} | Select-Object Name
I found this help, and I modify the previous command in this way:
Get-ChildItem -Recurse | `Where-Object {($_.directory -match '^*\\\S*$')} ` | ForEach-Object {?{ $_.PSIsContainer} | Select-Object Name }
but when I use it the PS doesn't display anything.
I can't understand why, someone can help me? Thank you!
Only files appear to have a .directory property, directories do not, so you will never get something which passes your (.directory matches a pattern) filter and also passes your (PSIsContainer) filter.
Except that your PSIsContainer filter doesn't work:
| ForEach-Object {?{ $_.PSIsContainer} | Select-Object Name }
this doesn't make sense; you can only filter the pipeline using ? with cmdlet | ? {}, you cannot filter at the start of a loop scriptblock with no input and get anything useful. This is running where-object {} over and over in a loop, - and that has no output.
Using -Recurse will be very slow, as you go into every single directory all the way to the end, and make [fileinfo] objects for all the files as well.
Apart from Matt's wildcard answer, assuming PS v3 or above, you could list all the directories in the root, and then all the directories inside those, and stop there:
Get-ChildItem c:\ -Directory | Get-ChildItem -Directory | Select -ExpandProperty Name
or
gci c:\ -Dir | ForEach { (gci $_ -Dir).Name }
You should just be able to use some fun wildcards to get what you want here.
Get-ChildItem \*\*\ | Where-Object{$_.PSIsContainer}
Or if you have at least PowerShell 3.0 this would be faster
Get-ChildItem \*\*\ -Directory
Then if you wanted just the names tack on | Select-Object -ExpandProperty Name
Here with full path, network compatible:
(Get-ChildItem "\\folder1\" -Dir).fullname | ForEach {(Get-ChildItem $_ -Dir).name}
Want it stored in an array?
$subfolders = (Get-ChildItem "\\folder1\" -Dir).fullname | ForEach {(Get-ChildItem $_ -Dir).name}

PowerShell script to copy files ordered by size

In a script that runs in the morning, I need to copy files from one server to another, and I plan to copy larger files first, and did so:
Get-ChildItem | sort -property Length -Descending | Copy-Item $LOCAL_BKP_TEMP\* -Destination $LOCAL_BKP_FINAL -PassThru
Gives the following error:
Copy-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
What should I do to make it work?
this should work:
Get-ChildItem | sort -property Length -Descending | Select -ExpandProperty FullName | Copy-Item -Destination $LOCAL_BKP_FINAL

Files sorting with version number in Powershell

I have this folder in a directory. With different version on them.
CD1,CD2,CD3,CD4,CD5,CD6,CD7,CD8,CD9,CD11,CD12
I'm new to powershell, can anyone help me to get the latest version folder from the above folders? Here CD12 is the latest folder. I can't use last modified time because I copy them at the same time.
$FolderName=(Get-ChildItem C:\Current\CD |Where-Object {$_.name -like "*CD*"}| sort{$_.name.Substring(2,2)}|Select-Object Name -Last 1).Name)
Write-Host "$FolderName"
I tried the above script and it did not help. Can anyone help me?
The next new version is CD13, and the script should get that folder
You can try something like below
$max_version = Get-ChildItem "C:\Current\" | Where-Object {$_.PSIsContainer}
| Foreach-Object {$_.Name} | Foreach-object {$_ -replace "CD", ""}
| measure -maximum | Select-Object -expand Maximum
Write-host ("CD" + $max_version)
Which will result in CD12
You almost have it. When I tried to run your code, I ran into two errors. First, you have an extra ')' at the end of the line causing a syntax error. Second, your 'SubString()' call is failing because you're trying to get the 3rd and 4th characters of a string without a 4th character ("CD1"). You don't need the scriptblock to your Sort command, though. You can just sort on the Name field.
$FolderName = Get-ChildItem C:\7005\Hot-Fix\CD | where Name -like "CD*" | sort Name | Select-Object -Last 1 -ExpandProperty Name
As a side note, this uses the PowerShell 3 syntax for Where-Object and Sort-Object to omit the {}. And it uses the -ExpandProperty parameter to Select-Object, so you don't have to wrap the whole thing in parens to get the Name property.
You could try this:
#requires -v 3
$baseFolder='C:\7005\Hot-Fix\CD'
$folder=dir $baseFolder\CD* -Directory |
? basename -CMatch 'CD\d{1,}' |
sort #{e={'{0:0000}' -f [int]($_ -replace '\D')}} -Descending |
select -First 1
Notice, I'm considering case sensitive matching; also, $folder contains what you're looking for.