Powershell scripting for a beginner help

Associate
Joined
15 Jul 2005
Posts
1,230
Location
UK
Hi all,

I have been learning Powershell this week and im slowly getting grips with it.

I cant seem to get the if statements working correctly as they just seem to skip them. the below script I wrote should just output some text if the UT4 folder is on my C:\ drive, but I keep getting the output of "thats a negative ghost rider! "

$path1 = "c:\"
$foldername = "UT4"
$whole = Get-ChildItem -Path $path1

if ($whole.name -eq $foldername)

{ Write-Host -ForegroundColor "green" Folder found! }

else

{ Write-Host -ForegroundColor "red" thats a negative ghost rider! }

I am guessing that the $whole.name part is wrong. Am I correct in saying that the dot after the variable should be the name of the membertype? I am getting the .name part when I run get-childitem | get-member

I am also having the same problem when I wrote this script

$wmiclass = "Get-WmiObject win32_processor"

if ($wmiclass.NumberOfCores -eq 4)
{Write-Host -ForegroundColor "green" Quad Core Baby!}

elseif ($wmiclass.NumberOfCores -eq 2)
{Write-Host -ForegroundColor "green" Dual Core Baby!}

elseif ($wmiclass.NumberOfCores -eq 1)
{Write-Host -ForegroundColor "green" Single Core Loser!}

else {Write-Host you have no CPU!}

Please note I don't have any experience in VB, perl etc, this is my first scripting language so my query is probably a rookie one!
 
I use something simular for cleaning up my movies folder once deleted the moving in XMBC since deleting the moving in XBMC only removes the .mkv file and not hte folder and other files. maybe this will help you


$MovieRoot = "\\Diskstation\Media\Movies"
$Extension1 = ".mkv"
$Extension2 = ".avi"

foreach ($MovieFolder in ((Get-ChildItem $MovieRoot).Name))

{

$ContainsFiles = get-childitem $MovieRoot\$MovieFolder -Recurse | Where-Object { ($_.Extension -eq "$Extension1") -or ($_.Extension -eq "$Extension2") }


if (!($ContainsFiles)) {
Write-host "The Following Movie Have Been Removed:" $($MovieFolder) -ForegroundColor Red
Remove-Item $MovieRoot\$MovieFolder -Recurse -Force #-whatif
}


}
 
Just while I was on lunch break...

$strFolderName="c:\UT4\"

If (Test-Path $strFolderName)
{Write-Host -ForegroundColor "green" Folder found!}

else

{ Write-Host -ForegroundColor "red" thats a negative ghost rider! }
 
Hmm, not sure why yours does not work Fishthrower, when i run your script on my machine with that folder on my C:\ it works:

PS C:\Users\itsys> $path1 = "c:\"
$foldername = "UT4"
$whole = Get-ChildItem -Path $path1

if ($whole.name -eq $foldername)

{ Write-Host -ForegroundColor "green" Folder found! }

else

{ Write-Host -ForegroundColor "red" thats a negative ghost rider! }
Folder found!
 
Thanks for looking into this :)

I have ran the folder script on another machine which seems to be working OK.. im using Powershell 3.0 on the working machine, the non working machine is using powershell 1.0. don't know if that's anything to do with it, or I was doing something wrong :P but it seems to be OK now!

the WMI script I fixed by removing the quotes on the variable on this line
$wmiclass = "Get-WmiObject win32_processor"

I am currently reading a PowerShell for dummies 1.0 book which is helpful. I will buy a more complex book after I read that after I get the simple stuff out of the way.
 
powershell 1.0 will certainly do it. Its old now and lots of stuff that you can do with powershell you just cant do with v1.
If you want to learn powershell then learn powershell 3.0 dont learn powershell 1.0
 
Back
Top Bottom