Skip to main content

Posts

Showing posts with the label PowerShell

PowerShell regex count specific character or number

Example input is: 0009911000


$Input_Number = '0009911000'

#[1] = tells regex to match on the right or look forward
$xtotal=[regex]::Matches($Input_Number,'0+')[1].Value.Length
#[0] = tells regex to match on the left or look backward
$ytotal=[regex]::Matches($Input_Number,'0+')[0].Value.Length

#Matches($Number,'0+') means #Matches($input, $pattern)

Write-Output $xtotal Write-Output $ytotal
Image output from PowerShell ISE:


Above code works or matches if input is something like this: “0009911000” There are zeroes on the left and right and in between are any numbers or characters.
For input like this: 0000991100010 in which zeroes are in different position of the string the below code will work.
$Input_Number = '0000991100010' $xytotal=[regex]::Matches($Input_Number,'[0]*[0]*[0]+') Write-Output $xytotal

Output will be something like this: Groups: {0000} Success: True Captures : {0000} Index: 0 Length: 4 Value: 0000
Groups: {000} Success: T…

Test AD account password without logging in to the computer

Documenting AD password is a good practice and should be a part of IT policy.
It’s easier to troubleshoot and support users if the password is known to IT. Of course, ethical use of the password should be followed.
How to test an active directory account password whether it’s working or not, without logging in or signing in on a computer?
PowerShell can help to test a user account password quite easily.
AD admin account is needed and the PowerShell must be elevated while running the script below. Correct version of PowerShell is needed for this script.
See image below:


That’s it! Just replace the username and password to be tested and you can tell your users that the password is not working and needs password reset for their account to work.
The script is written for educational purposes and used it ethically.
Cheers!! Till next time. Happy Coding!!!
================================ Free Android Apps:
Click  links below to find out more:
Excel Keyboard guide: https://play.google.com/store/apps/det…

Error: the type or namespace name does not exist in the namespace using visual studio 2017

When compiling an error will occur that a name space does not exist. An example error like the message below:
The type or namespace name 'Microsoft.Office.Interop.Outlook' does not exist in the namespace 'Microsoft'(are you missing an assembly reference?)
The system is trying to find a reference for a specific namespace or dll.
One solution for this is to add the reference to the missing namespace.
Find the “solution explorer” window like the image below:


After opening the “solution explorer” window, right click the name of the project just below the solution explorer. Beside the C# icon.
A new window will open like the image below. Click “Add”, then click “Reference”.



After clicking “Reference”, the “Reference Manager” window will open.
Click “COM” option, then use the filter option to go directly to the desired namespace or reference. Since the error is a Microsoft namespace, type “Microsoft” and “Microsoft” namespace will be filtered from the list. See image below…

PowerShell Insert Multiple Lines or text to another file dynamically

Copying and pasting text to another file is a common thing to do, copy and paste done. Boom!
In PowerShell scripting copying and pasting text from one file to another dynamically isn’t as easy as it is done in a spreadsheet or document. Dynamically while the code is running  and doing things in the background without human intervention.
As the odds say, if there’s a will there’s a way. And making things efficient and easy is a good way in life.
If ever a scenario occurs that you need to copy text or lines in a file from one file to another, without breaking the code execution. Script below works just fine. Just insert the code where it is needed.

PowerShell code snippet:
#================================
$reader=[System.IO.File]::OpenText("D:\test-read-paste\main1.txt") #get-content can also be used
$lineNumberx=3 #adjust this number where you need to start inserting the text
#Loop line by line and process while($null-ne ($line=$reader.ReadLine())) {
$textToAdd=$line
$newLineToAd…

PowerShell repeat spaces or characters

Example code of repeatingspaces within a string:
$xrepeat = ' ' * 11 $string_with_spaces = “ Hello World of Spaces!!!”
Write-output $xrepeat $string_with_spaces
Output image:

To indent or tab a string:
$Tab = [char]9 Write-Output "$Tab Hello Tab Indention!!!"
Output image:

To add other HTML character in PowerShell string:
$Euro_symbol = [char]8364 Write-Output "$Euro_symbol Hello Euro Symbol in PowerShell!!!"
Output image:


================================ Free Android Apps:
Click  links below to find out more:
Excel Keyboard guide: https://play.google.com/store/apps/details?id=chrisjoms.myexcelapplicationguide

Heaven's Dew Fall  Prayer app for Android :
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html

Working with text in PowerShell

Working with a text file or text format in PowerShell is a good thing to learn.
Getting or monitoring output from PowerShell for further processing or analysis and having it in text file format can easily be imported to other applications.
Working with text or other format, requires these basic operations: Save, Overwrite, Append and Save As.
Save, overwrite, append and save as is a common operation when editing word documents, excel file and other office applications since it’s freely available on the menu of the application. But can we do this in PowerShell? The answer is a resounding, yes.


So, how to do it? Examples below shows how to work with text files in PowerShell and do the operation mentioned above.

Equivalent operations for Save, Overwrite, Append and Save As in PowerShell:
===Set-content ==== will overwrite the file ===Add-content ==== will just add or append something to the file ===Out-File ==== is like "Save As" since you have the option to save the file to a…

Change Password of Scheduled Tasks in PowerShell

How to change Tasks Scheduler password?
How to set a new password for scheduled tasks in Windows Task Scheduler using a script?
PowerShell is always a friend to cure laziness in an effective and efficient way.
Of course, not all things can be done by automation. Sometimes manual method is more applicable and a secure method on some scenario.
If you have a few servers that has scheduled tasks on it and need to change the password occasionally to comply with cyber security policies, changing tasks manually might be painful and chances are some tasks might be missed.
Changing the password in Task Scheduler using PowerShell is an efficient method if the task has to be done repeatedly on a few servers.
To change the password, the TaskName must be known since it’s an input or a required field for the command used to change the Task password.
To get Tasks Name use the PowerShell code below:
Get-ScheduleTask - TaskPath "\"| Get-ScheduledTaskInfo
The TaskPath refers to location of the Task Na…

PowerShell Ping IP get status or reply

How to ping sweep a network?
How to ping sweep a subnet?
There’s a lot of ways to ping the whole subnet or do a sweep ping to the whole network, of course download any free software that can do this, or just manually ping the whole network by manually changing the IP address.
Or do an old school method, using a batch file. This link shows how to ping the whole subnet via command line. http://quickbytesstuff.blogspot.sg/2015/11/how-to-ping-whole-subnet-using-batch.html
Since PowerShell is readily available from Windows 7 up to the newer versions of Windows. Then PowerShell is the perfect choice to do this task.
Open PowerShell ISE or the PowerShell command line, then type or copy the commands below:
$ping = New-Object System.Net.Networkinformation.Ping 1..254 | % { $ping.send(“192.168.1.$_”) | select address, status | ft -auto }

Change the IP Address (192.168.X.X) to the IP address range of your network.
What the script does, is to Ping the whole network starting from 1 to 254, basically the w…

Check PowerShell Version

How to check PowerShell version?

Open PowerShell ISE or PowerShell command line and type: get-host | select version

Sample Output:
Version: 2.0

Why need to check the PowerShell version?

If some commands that you find online is not working, it's either the command is wrong or the command is available for specific versions only.

New commands will work on new version of course, old commands may not work also on newer version if the command is obsolete or deprecated.

As the odds say, "Change is the only permanent thing in this world." and this quote is quite applicable in today's digital world.


Till next time. Cheers!

================================ Free Android Apps:
Click on links below to find out more:
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Excel Keyboard shortcuts guide http…