Skip to main content


Showing posts with the label PowerShell

PowerShell switch case call function

Code below collect user input and uses switch case statement to check if the input matches, and if it matches a function is called that will execute commands.Here’s the code:#get or read from user input$computer_name=read-host ("Enter Computer Name:")switch ($computer_name ){#if the input is computer_1 then function func_comuputer1 is called computer_1{func_computer1}computer_2{func_computer2}}#function called if computer_1 is the inputfunctionfunc_computer1 {write-host"You entered Computer_1"#or replaced with other function like reboot / shutdown /or other commands#Restart-Computer -ComputerName computer_1}
#function called if computer_2 is the inputfunctionfunc_computer2 {write-host"You entered Computer_2"#or replaced with other function like reboot / shutdown /or other commands#Ex: Enter-Pssession computer_2}

Code below will keep asking for input and will only exit if a specific string matches to exit the script. Here’s the code:do {$computer_name

How to upload to S3 using PowerShell

Uploading to S3 using PowerShell is quite easy.Here’s the code:$accesskey='123'
$dregion='us-east-1'Write-S3Object-AccessKey$accesskey-SecretKey$secretkey-Region$dregion-BucketName-FileC:\Temp\test-file.txt#Replace NameofthebucketinS3 with the folder name found in S3#test-file.txt is the file that will be uploaded to S3 and is located on c:\Temp#Change the folder location c:\Temp and the file to be uploaded #Above code will upload the file to the root of the S3 Bucket#If need to upload a file to a specific subfolder in S3 bucket used the code below$accesskey='123'
$dregion='us-east-1'Write-S3Object-AccessKey$accesskey-SecretKey$secretkey-Region$dregion-BucketName NameofthebucketinS3/SubFolderBucket-FileC:\Temp\test-file.txt

PowerShell launch an application and close it automatically

Code below will run an application and PowerShell will also close it after a few seconds.$process_id=Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='cmd /c "C:\dev\remind.bat"'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5stop-process$process_idSample contents of remind.bat:
@echo off
echo "Shutdown the server. Do it now."
==================Notepad is on the batch file so the command window will stay on the top of any running applications.Sample output of the above code:
Code below will run calculator and close after a few seconds specified on the code:Invoke-CimMethodWin32_Process-MethodName"Create"-Arguments @{CommandLine ='calc.exe'} |Select-Objectprocessid|ft-HideTableHeaders|Out-StringStart-Sleep-Seconds2.5$get_process_id=Get-Process-Namecalcu*|<

PowerShell restart remote server with confirmation

Restart a remote PC or server and ask user to confirm reboot before proceeding.

Here’s the code:
$response_confirmation=[Microsoft.VisualBasic.Interaction]::MsgBox("Restart this server: "+$server_q,'YesNo',"Confirm to reboot?")

switch ( $response_confirmation )
Yes{ Restart-Computer-ComputerName$server_q} No{ Write-Host"Restart Cancelled"} }
Code above will prompt the user with “Yes” or “No”, yes will send the restart or reboot command to the remote server or computer, and no will cancel or exit the execution.

PowerShell code above also demonstrate how to use a switch case statement and use the response to perform an action, in regar

PowerShell check access or folder permissions

In an Active Directory domain, one of the common issues is folder permissions. Of course, permission must be restricted as much as possible. In order, confidential data or things that only a group of people will know, is not made available to everyone.

Employee salaries on a network share made available to everyone, will cause some employees to be disheartened. Such data, the access or folder permission should be checked properly.
One way to check a folder permission is to assume or login as a certain user that is not supposed to have access to such data and checked whether the data can be viewed or not.
Aside from restricting permissions for confidential data; data supposed to be accessed by everyone but some user’s complaint that they are unable to access then this will also cause some problem and may end up not being productive for the users.
One way to check is to go to the user’s workstation and verify whether the user is unable to access. This kind of issue is not hard to solve…

PowerShell Get OneDrive email address

If you have a subscription to Office 365, chances are you have OneDrive configured in your system and is linked to your email address.
OneDrive is quite good since you can have your data on the cloud, of course you also have to use common sense and what data you are uploading to the cloud.
OneDrive will come handy since you just need internet connection and you will be able to retrieve your notes or data anywhere.
But how to check which email address is linked to your OneDrive? Well, there' a simple way using PowerShell to view registry keys.
A one liner PowerShell script will come-in handy to do these kind of task.
Here’s the code, if using Hotmail, or Live email:
For OneDrive Business use this code: