Difference between revisions of "Windows Powershell"

From rbachwiki
Jump to navigation Jump to search
Line 40: Line 40:
  set-location 'g:\box\All Flds\web\amazon\test'
  set-location 'g:\box\All Flds\web\amazon\test'
== Search for Multiple files==
== Search for Multiple files==
  Get-ChildItem | Where-Object {$_.Name -like "*catch*" -or $_.Name -like "*dream*" -or $_.Name -like "*david*"}
  Get-ChildItem | Where-Object {$_.Name -like "*catch*" -or $_.Name -like "*dream*" -or $_.Name -like "*david*"} > output.txt


==Eject Drive *Not tested==
==Eject Drive *Not tested==

Revision as of 20:58, 27 January 2021

Script that create a folder based on today's date, then copy the contents of one folder to another

$datecurrent = get-date -Format MM-dd-yyyy
$apath = "C:\Users\John\Desktop\leaves"
$btest = "C:\Users\john\Desktop\leaves\pdf-sliced"
$ctest = "C:\Users\john\Desktop\leaves\atest"

#create directory
if ( -not (Test-Path "$ctest\$datecurrent" -PathType Container) ){
New-Item -ItemType directory -Path "$ctest\$datecurrent"
}else{
"Directory already exist! Maybe you ran the program twice"
Read-Host -Prompt "Hit Enter to Exit"
break
}


if( Test-Path "$ctest\$datecurrent" -PathType Container ){
#open folder
ii "$ctest\$datecurrent"
#copy files from source to destination
Copy-Item -Path $btest\* -Destination "$ctest\$datecurrent" -Verbose

}else{
"There Was an error - Contact Robert"
}
Read-Host -Prompt "Hit Enter to Exit"

save with .ps1 extension

Copy all files with specific extension

$datecurrent = get-date -Format MM-dd-yyyy
$atest = "C:\Users\Robert\Desktop\leaves"
$ctest = "C:\Users\Robert\Desktop\leaves\atest
Get-ChildItem -Path "$atest\*" -Include *.pdf -Recurse | Copy-Item -Destination "$ctest\$datecurrent" -Verbose

CD to a specific directory

set-location 'g:\box\All Flds\web\amazon\test'

Search for Multiple files

Get-ChildItem | Where-Object {$_.Name -like "*catch*" -or $_.Name -like "*dream*" -or $_.Name -like "*david*"} > output.txt

Eject Drive *Not tested

$vol = get-wmiobject -Class Win32_Volume | where{$_.drivetype -eq '2'}  
$Eject =  New-Object -comObject Shell.Application
$Eject.NameSpace(17).ParseName($vol.driveletter).InvokeVerb(“Eject”)

Category