Category Filter

Code snippets for the most commonly executed tasks in Windows

You can execute custom scripts on Windows 10 running version 1703+, enrolled in Hexnode using the Hexnode Installer App. Hexnode supports .bat, .cmd, .ps1 script formats. Here are some sample code snippets for the most commonly executed tasks in Windows that you might find useful:

Disclaimer:


THE SAMPLE SCRIPTS PROVIDED BELOW IS ADAPTED FROM THIRD-PARTY OPEN-SOURCE SITES.

Note:


.cmd files varies with .bat file only in syntax.

To restart the device

.bat format .ps1 format
shutdown.exe /r /t 00 Restart-Computer -Computername "computer_name" -force

To shutdown the device

.bat format .ps1 format
shutdown.exe /s /t 00 Stop-Computer -ComputerName "computer_name" -force
Note:


By making changes to the last part of the command, you can delay the process. To restart the device in 15 seconds,

shutdown.exe /r /t 15

To rename a particular file

.bat format .ps1 format
 
cd location_of_the_file 
 ren oldname newname
 rename-item  "current_filename_with_path.ext" "new_filename_with_path.ext" 

E.g. To rename the file test.txt in the Desktop of the user Deborah to testing.txt,


cd C:\Users\Deborah\Desktop

ren test.txt testing.txt

E.g. To rename the file test.txt in the Desktop of the user Deborah to testing.txt,


rename-item “C:\Users\Deborah\Desktop\test.txt” “C:\Users\Deborah\Desktop\testing.txt”

Note:


Use quotes if the name has a space in between.

To delete a file/folder

.bat format .ps1 format
To delete a file
 
del “filename_with_location.ext”  
To delete a file
remove-item “filename.ext” 

E.g. To delete the file test.txt in the Desktop of the user Deborah,


del “C:\Users\Deborah\Desktop\test.txt”

E.g. To delete the file test.txt in the Desktop of the user Deborah,


Remove-item “C:\Users\Deborah\Desktop\test.txt”

To delete a folder
 
rmdir /Q/S “foldername_with_path”  
To delete a folder
 
remove-item “foldername_with_path” -recurse   

E.g. To delete a folder test in the Desktop of the user Deborah,

rmdir /Q/S “C:\Users\Deborah\Desktop\test”

Note:


/S deletes all the files from the folder. /Q makes sure that the user is not asked for confirmation for deleting the folder.

E.g. To delete a folder test in the Desktop of the user Deborah,

remove-item “C:\Users\Deborah\Desktop\test” -recurse

Note:


The –recurse attribute ensures that all the files inside the folder is deleted.

To uninstall Windows Store apps

.bat format .ps1 format
 msiexec /x "path_to_msi" /qn 
Get-AppxPackage “*name of the app*” | Remove-AppxPackage 

E.g. To remove the app Notepad saved in C drive,


msiexec /x "C:\TEMP\XmlNotepad.msi" /qn

E.g. To remove the system app Alarms & Clock,


Get-AppxPackage * Microsoft.WindowsAlarms * | Remove-AppxPackage

To uninstall Enterprise apps

.bat format .ps1 format
To uninstall .msi and .exe files,
wmic product where "name like '%%product_name%%’'" call uninstall/nointeractive 

Note:


msiexec can also be used to uninstall msi files.

To uninstall .msi and .exe files,
 $app=Get-WmiObject –Class Win32_Product –Filter “Name like ‘%product_name%’” 
$app.Uninstall() 

E.g. To remove the enterprise app 7-Zip,


wmic product where "name like “'7%%'" call uninstall/nointeractive

E.g. To remove the enterprise app 7-Zip,


$app=Get-WmiObject –Class Win32_Product –Filter “Name like ‘%7%’”


$app.Uninstall()

Copying to clipboard

.bat format .ps1 format
 echo|set/p=”text_to_copy”|clip  
Set-Clipboard –Value “text” 

E.g. To copy the text hello to clipboard,


echo|set/p=”hello”|clip

E.g. To copy the text hello to clipboard,


Set-Clipboard –Value “hello”

Creating a user

.bat format .ps1 format
 
Net user username password /add 
New-LocalUser –Name “username” -NoPassword 

E.g. To create a user Josh,


Net user Josh “” /add

E.g. To create a user Josh,


New-LocalUser –Name “Josh” -NoPassword

Note:


The added user might not be visible at the time of login or in Settings > Accounts > Family and other users. To make them visible, add the users to the group.

To add a user to the group

.bat format .ps1 format
 
net localgroup group_name UserLoginName /add 
Add-LocalGroupMember –Group “group_name” -Member “name_of_the_user”  

E.g. To add the user Josh to the group of Users,


net localgroup Users Josh /add

E.g. To add the user Josh to the group of Users,


Add-LocalGroupMember –Group “Users” -Member “Josh”

Change password

.bat format .ps1 format
 
Net user username password 
$NewPassword = ConvertTo-SecureString "the new password" -AsPlainText –Force 
Set-LocalUser -Name  -Password $NewPassword 

E.g. To edit the password of the user, Josh to abc_123,


Net user Josh abc_123

E.g. To edit the password of the user, Josh to abc_123,


$NewPassword = ConvertTo-SecureString "abc_123" -AsPlainText –Force
Set-LocalUser -Name Josh -Password $NewPassword