Skip to main content

Posts

Showing posts from April, 2020

PowerShell get startup items or files

Checking startup items or files, when Windows is starting or upon user successful login is quite important. Some files need to run on startup so it can be used while the user or computer is being in used. Or the startup files is set, so when the computer starts the file or items will also start. In this way, you don’t have to remember what things to open when the program or items will run during startup. Of course, it’s not only the important files will be set during startup. Some viruses or malware sometimes use this method to load the software, so that they will also be in the system as long as the user is logged in. So, how to check in PowerShell to get all those startup items or files? Here’s the code: Get-CimInstance Win32_StartupCommand | select Description , Command , Location , User , Caption Sample output: Description : OneDrive Command      : "C:\Users\duser_name\AppData\Local\Microsoft\OneDrive\OneDrive.exe" /background L

PowerShell get time zone setting

Time zone settings in computers has a lot of purposes, of course it can tell you the time when to knock off from work. 😊 But there’s a lot of other purposes and having a time zone that is different from software application that is expecting, then unexpected issues may arise. Database must be in sync with a correct time zone or else time stamp on database records will be inaccurate, system logs must also be in a correct time zone or else when diagnosing the logs or data will be an issue. There are other issues that requires to have a correct time zone setting set on your computer. Checking whether the correct time zone is set on your computer can easily be done in PowerShell. So, how to get time zone settings in PowerShell? Here’s the code: Get-CimInstance Win32_TimeZone | select caption Windows 10 does support a setting with multiple time zone, here’s an output from above PowerShell with two time zone set on a computer. Sample output: caption        

Get PowerShell version on remote computers

PowerShell version on remote computers must be installed with the latest version if possible. It would make life easier. New version comes with of course, new features and more fun. And if all servers or PCs has the same PowerShell version, then you need to create one version of PowerShell that works for all computers or servers. But how to get PS version on remote computers? To get the version locally, it’s quite straight forward: Method 1: ( $Psversiontable ) . PSVersion Method 2: ( get-host ) . Version To get version on remote computers, just add a few lines from the command above. #--------- save as Get_PS_Version.ps1 or any file name $xversion = ( get-host ) . Version $xversion_output = "PowerShell Version: "   + $xversion $pc_name = $env:computername $pc_output = "Computer Name: " + $pc_name $xversion_output + "`n" >> c:\scripts\output\ $pc_name .txt $pc_output >> c:\s

How to check office version from command line

The are quite a few ways to check office version it can be done via registry, PowerShell or VBScript and of course, good old command line can also do it. Checking Windows office version whether it is Office 2010, Office, 2013, Office 2016 or other version is quite important to check compatibility of documents; or just a part of software inventory. For PowerShell this simple snippet can check the office version: $ol = New-Object -ComObject Excel.Application $ol . Version The command line option will tell you where’s the path located; the result will also tell whether office is 32-bit, 64-bit and of course the version of the office as well. Here’s the command that will check the office version and which program directory the file is located which will tell whether it’s 32-bit or 64-bit. Command to search for Excel.exe: DIR C:\ /s excel.exe | find   /i "Directory of"  Above command assumes that program files is on  C: drive. Sample Outpu

PowerShell with regex to rename files

Renaming files is quite an easy task but if the files to be renamed are about 100 files or more then you have to think twice and is not fun doing repetitive simple task. Anyway, PowerShell is handy for this kind of task. Regex in PowerShell is helpful in order to filter only matching files that needs to be renamed and leave any other files that doesn’t need  to be changed. Example filename is like this: SampleTestData1_2019Result.csv New desired filename would be:   SampleTestData1_2020.csv PowerShell code with regex to rename matching files. How to use basic regex in PowerShell? The code below is a simple one. Get-ChildItem   -Path "c:\dev" | Where-Object { $_ . BaseName -Match '2019.*' } |   Rename-Item -NewName {( $_ . BaseName -Replace '2019.*' , '2020' ) + $_ . Extension}   #'2019.*' – is the regex that matches 2019 until the end of the string #'2019.*' , '2020' – replace the mat

WMIC command get CPU Load Percentage

Getting CPU load percentage is quite helpful in determining whether the server or computer is on a heavy performance, this would also indicate whether there is a need to increase the memory or add another CPU to the machine. Of course, when the CPU has a lot of load, performance will be impacted directly. Which basically means that the machine will not be working perfectly. Checking the CPU load will also help to troubleshoot, if the services on the machine or server is experiencing slow performance. Here’s the wmic command line which check the CPU Load Percentage. wmic cpu list status Sample output: Availability   CpuStatus   CurrentVoltage   DeviceID   ErrorCleared   3                              1                      9                                       CPU0                     ErrorDescription   LastErrorCode   LoadPercentage   Status   StatusInfo                                                                                    15         

Excel VBA copy and paste a value or data

Copying and paste in Excel is quite simple and straight forward. It should be the same thing with VBA, there are few ways to do this via VBA. The simple is to record a Macro then check the code, then alter the data to suits the operation that needs to be done. Basically, when copying and paste; it’s like pointing the data to another cell or cells so both locations will have the same value or data. The code example below, does the above strategy. Get the value of the data that is to will be copied and point the data to a location where the data will be pasted. Here's the Excel Vba code to copy and paste: Sub CopyandPaste() Set wb2 = ThisWorkbook 'Copy or set the value to be copied to a variable string_row_value = wb2.Sheets("Sheet1").Cells(1, 1).Value 'Paste or set the value to Sheet2 wb2.Sheets("Sheet2").Cells(1, 2).Value = string_row_value 'If needs to be copied to the same worksheet or in sheet1 itself

PowerShell Date Formatting

How to format a date in PowerShell? There are a lot of methods to do this and two easy way, the easy or the hard way. Easy way are the methods below: #Method 1 $year = (get-date -UFormat "%D") Write-Output ("Method 1: " + $year + "`n`r") #Method 2 $year = (get-date -UFormat "%m-%d-%y") Write-Output ("Method 2: " + $year + "`n`r") #Method 3 $year = (get-date -UFormat "%d-%m-%y") Write-Output ("Method 3: " + $year + "`n`r") #Method 4 $year = (get-date -UFormat "%d/%m/%y") Write-Output ("Method 4: " + $year + "`n`r") Sample output: Method 1: 04/23/20 Method 2: 04-23-20 Method 3: 23-04-20 Method 4: 23/04/20 Image output below: For a complete year output, use capital Y. $year = (get-date -UFormat "%d-%m- %Y ") Write-Output ("Method 5: " + $year + "`n`r") Output: M