I'm trying to use the script given in this answer Setting window size and position in PowerShell 5 and 6
to set the height and size of multiple windows explorer windows. Not the internet explroer.. the file browser called 'explorer'.
it works with the program 'notepad'. but not with the program 'explorer'.
#works
Set-Window -ProcessName notepad-X 400 -Y 400 -Width 400 -Height 700
#doesnt work
Set-Window -ProcessName explorer -X 400 -Y 400 -Width 400 -Height 700
ideally I'd like to have a script:
- open 3 explorer windows.
- navigate to filepath A,B,C
- resize each window to a specific location on the screen
How can I do this without installing any extra software and just use raw powershell here?
EDIT: After using harrymc's suggestion, I've gotten halfway through the problem.. I can move the window but I just need to figure out how to get the handle of 3 explorer child processes...
$MethodDefinition = @'
[DllImport("user32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
# How do I get 3 child explorer IDs here?
# i can't pass in 'explorer' name because that references the parent process running the whole GUI
$Handle = (Get-Process -Name "notepad").MainWindowHandle
$Return = [Window]::MoveWindow($Handle, 10, 20, 400, 400,$True)
Edit 2:
I've tried getting the explorer window through the Start-Process function but I am receiving an error:
$er3 = (Start-Process explorer -passthru)
PS C:\> (Get-Process -Id $er3.Id).MainWindowHandle
Get-Process : Cannot find a process with the process identifier 10572.At line:1 char:2
+ (Get-Process -Id $er3.Id).MainWindowHandle
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (10572:Int32) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand
it says it has exited... but the explorer filebrowser window stays open... not sure whats going on here. If i try it with notepad it works...
$er4 = (Start-Process notepad -passthru)
PS C:\> (Get-Process -Id $er4.Id).MainWindowHandle
9899994
Edit 3: I've figured it out using the ComObject, and accessing item(0).
$ex4 = New-Object -ComObject Shell.Application
$ex4.open("C:\")
# $ex4.windows()[0].Width = 400 # breaks
$ex5 = $ex4.Windows()[0]
$ex6 = $ex5.Item(0) # not sure why i need to do this extra step
$ex6.Width = 400
$ex6.Navigate("file:///C:/Folder1/Folder2")