Skip to main content

Posts

Showing posts from June, 2017

Use PowerShell to list installed programs

Do you need to check list of programs installed on remote computers? PowerShell will come in handy to check list of installed software on remote computers. Open PowerShell ISE or the PowerShell command line. Make sure the PowerShell settings is configured to run PowerShell scripts. Here's the PowerShell code snippet. #for 64 bit  #Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize #for 32 bit  Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.publisher -notmatch "^Microsoft"} | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize To get the list of non-Microsoft software installed on the computer:   Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.publisher -notmatch "^Microsoft"} | Select-Object DisplayName, ...

PowerShell create active directory user

Create bulk users for Active Directory in PowerShell with the help of a text file or a CSV file can be done easily. But the code below is to create a single user account. The code below can easily be tweak by using for loop command and reading an external file where the data will be imported to PowerShell variables. When I was creating this script, I encountered an error that says: new-aduser attribute value was not in the acceptable range Basically, the error is good and is useful as well. Since it already gives a hint that one of the value is not acceptable. And on this case, it was the Country variable, I was writing the whole word “Philippines” when I changed it to PH and the error went away. Here’s the code: #------Code Start--------  $Domain="@gui.local" $Firstname="Yorem" $Lastname="Goas" $Email="yorem.goas@gui.com" $Password="Wh@t!s1t0k" $Username=$Firstname +"."+ $Lastname $UPN=$U...

PowerShell remove line feed

Line feed is useful in some ways like formatting lines so it will be easy to read if there’s a line in between the other line. But there are also instances that line feed might be annoying or simply we need to get rid of it since it’s not desired on the output. There instances that Out-string parameter in PowerShell will append a line feed on its output. Or if you take the input or output from other sources but want to remove the line feed, PowerShell can remove the line feed without any hassle. Code below is using PowerShell to remove line feed from an input, the input could either be a text file or a string pipe to another variable. $string_with_line_feed -replace "`n|`r" `n -  backtick with n refers to the line feed character `r – backtick with r refers to the carriage return Or the code above can be written as follows: $string_with_line_feed -replace "`n|" So, basically line feed is replace with nothing. But for readabili...

Find or search files in Windows

Search files recursively using PowerShell or the old good dir command line. To search using PowerShell: gci d:\ -recurse | where-object {$_.name -like "g*.txt" } GCI – a short form for Get-ChildItem Recurse – is to search recursively (folder and sub-folders) G*.txt - * tells the PowerShell to search any document that start with g and don’t care anything that follows it To do it using the old way via command line, open command prompt and type: Dir /s d:\ g*.txt To save the search output just use the “>” redirect operator. Dir /s d:\ g*.txt > d:\G_filenames.txt /s - parameter to search recursively (folder and sub-folders) The search result will be written to G_filenames.txt In PowerShell to save the search result type: gci d:\ -recurse | where-object {$_.name -like "g*.txt" } | out-file d:\gxfiles.txt If you still want the very conventional way of searching. Minimize all windows folders by pressing "w...

PowerShell Test Port Forwarding - check if port is open

Port forwarding is necessary if the device is that needs to connect to the unsecured world of the Internet. Of course, before opening port or doing any port forwarding make sure that security is in place. Firewall rules, software configuration are properly set, up-to-date anti-virus and other settings that needs to be done to secure the system. PowerShell can test whether a remote port is open or a Port Forwarding rules on either on the Firewall or router is set correctly. Here’s a one liner code, to check whether the Port is open or not. Both one line code below does the same thing, check whether the Port is open or not. 1 . New-Object System.Net.Sockets.TCPClient -ArgumentList "Remote.Public.IP.Address", 3389 2. Test-NetConnection -Port 80 -InformationLevel Detailed Replace the port number, with any port number to be tested. If the connected property on the displayed output is true, then the port is open. More details on the link b...

Linux bash script get folder size

Having an error, no space left on the device? But you don't know which folder is consuming the most space? Bash script below will get all the folder size and display the size for each folder. How to use the script below? Open your favourite terminal editor, vi, vim or other editor. At terminal, type: vi dirfoldersize.sh Copy and paste the code below to the editor. Save the file. Then make the script executable, chmod +x dirfoldersize.sh Run the script by typing, ./dirfoldersize.sh If everything goes well, you will get the folder size for each folder on the directory where the script runs. See sample output below. =============== #code begins start copy below #!/bin/bash curdir=$(pwd) compath="$curdir/dirlist.txt" ls -l | grep ^d | awk '{print $9}'  > $compath while IFS= read -r var do   foldersize=$(du -sh $var |awk '{print $1}')   echo "$curdir/$var folder size: $folders...

Powershell move selected files

Powershell script below will sort files base on its last write time, it will select the files and move the selected files to another folder. This script will remove the first 3 old files base on the lastwritetime property, quite useful to archive files or grouping large files into multiple folders. #=====Code Snippet Start $xfiles= Get-ChildItem d:\the_log_folder |  Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true} | Select-Object -first 3 | select fullname | ft -HideTableHeaders write $xfiles >  d:\move_files_listing.txt ForEach ($movefiles in (Get-Content "d:\move_files_listing.txt")) {        If ($movefiles[1].Length –gt 0) {       move-item $movefiles d:\the_archive_folder       write $movefiles      } } #=====Code Snippet Ends Code explanation: --# Sort-Object -Property @{Expression={$_.LaswriteT...

Symantec endpoint protection deployment fails

Deploying Symantec Endpoint Protection or SEP 14 on remote computers will fail if Firewall or services on remote computer is not properly set. Deploying SEP version 14 to computers running Windows 8.1 will not install successfully. If ICMPV4 is not allowed on the firewall will also cause fail deployment. Even though ICMPV4 is enabled and allowed on the firewall and the deployment still fails. If unable to deploy SEP 14 to remote computers running Windows 8.1, either do manual installation, send a link to user and ask them to install if user has a local admin password. Of course, if trying to install to multiple computers remote deployment is the best way to do. It's efficient, faster and user productivity is not disturbed. One solution for this issue, is to enable "remoteregistry" service on the remote computers. To enable "remoteregistry" on remote computers, open an elevated command prompt and enter the domain credentials. Type: C:\>sc ...