Friday 31 July 2020

powershell 8 logic

PS C:\Users\bob\ps> $path = 'c:\users'
if (Test-Path $path) {
    write-host "$path verified"
}
else {
    write-host "$path not verified"
}
c:\users verified

PS C:\Users\bob\ps> [int]$value = Read-Host 'Enter a number'
switch ($value) {
    1 {
        Write-Host 'You entered the number one'
     }
     2 {
        Write-Host 'You entered the number two'
     }
     3 {
        Write-Host 'You entered the number three'
     }
     4 {
        Write-Host 'You entered the number four'
     }
     5 {
        Write-Host 'You entered the number five'
     }
    Default {
        Write-Host "I don't know what to do with $value"
    }
}
Enter a number: 3
You entered the number three

PS C:\Users\bob\ps> for($i = 0; $i -le 15; $i++){
    Write-Host -NoNewline $i ' ' -ForegroundColor $i
}



$path ='C:\Users\bob\Desktop\7.30'
[int]$totalSize = 0
$fileInfo = Get-ChildItem $path -Recurse
foreach($file in $fileInfo){
    $totalSize += $file.Length
}
Write-Host "Total size of file in $path is $($totalSize/1Mb) MB"
Total size of file in C:\Users\bob\Desktop\7.30 is 374.533009529114 MB

PS C:\Users\bob\ps> $pathVerified = $false
do{
    $path = Read-Host 'please enter a file path to evalute'
    if(Test-Path $path){
        $pathVerified = $true
    }
}while (!$pathVerified)
please enter a file path to evalute: c:\user
please enter a file path to evalute: c:\users

PS C:\Users\bob\ps> $pathVerified = $false
while (!$pathVerified) {
    $path = Read-Host 'please enter a file path to evalute'
    if(Test-Path $path){
        $pathVerified = $true
    }
}
please enter a file path to evalute: c:\user
please enter a file path to evalute: c:\users

PS C:\Users\bob\ps> $path = 'c:\users\bob'
$folderCount = 0
Get-ChildItem $path | ForEach-Object -Process {if ($_.PSIsContainer) {$folderCount++}}
$folderCount
65

reference:
https://www.youtube.com/watch?v=nesN4Iznbco

No comments:

Post a Comment